Share

1.5 Import and Format Datasets

  1. Select File-Change dir....
    Select folder that you are working in that includes your dataset
  2. Determine the name of your file ("temp" in our case here)
    We can then enter the path with this name to bring our dataset into R

    temp <- read.csv("C:\\Walter\WalterSpatialEcologyLab\\SpatialEcologyCourse\\
      Chapter1\\TimeLagCode\\Y2005_UTM_date.csv", header=T)
  3. We can also just open a new R document and save the workspace in the TimeLagCode folder so that the working directory can be set automatically whenever you open the R document from this folder using the code

    temp <-read.csv("Y2005_UTM_date.csv", header=T)
  4. It is often necessary to determine the time lag between successive locations within your dataset

    # modify time to include seconds
    temp$time <- paste(as.character(temp$LMT_TIME),"00",sep=":")
    # convert to chron date
    temp$date_time <- chron(as.character(temp$LMT_DATE),
    temp$time,format=c(dates="m/d/y",times="h:m:s"))
    # calc diff in minutes
    timediff <- diff(temp$date_time)*24*60
    # remove first entry without any difference
    temp <- temp[-1,]
    # assign timediff column
    temp$timediff <- as.numeric(timediff)


    The above code will result in a dataset that includes "timediff" that is the time between successive GPS points
    Location date_time difference
    1    
    2 (07/13/05 10:00:00) 7
    3 (07/13/05 10:30:00) 30
    4 (07/13/05 11:00:00) 30
    5 (07/13/05 11:30:00) 30
    6 (07/13/05 12:00:00) 30
    7 (07/13/05 12:30:00) 30
    8 (07/13/05 13:00:00) 30
    9 (07/13/05 13:30:00) 30
    10 (07/13/05 14:00:00) 30
    11 (07/13/05 15:00:00) 60
    12 (07/13/05 15:30:00) 30
    13 (07/13/05 16:00:00) 30
    14 (07/13/05 16:30:00) 31
    15 (07/13/05 17:00:00) 30
    16 (07/13/05 17:30:00) 29
  5. We can then either export this file as an excel file for use in other programs or rename the output it to use it in R in subsequent analysis

    write.table(temp,"C:\\Walter\\WalterSpatialEcologyLab\\SpatialEcologyCourse\\
    Chapter1\\TimeLagCode\TimeDiffdata.csv", row.names=TRUE, sep=" ",
    col.names=TRUE, quote=TRUE, na = "NA")
  6. The output data will be in the TimeLagCode folder in the .csv table and will include more than we need. We can manipulate the data further in R or use "text to columns" in Excel to get the time between successive locations that we were aiming for here.