[R] Read in data table, change columns from factors

Rich Shepard r@hep@rd @end|ng |rom @pp|-eco@y@@com
Thu Jul 19 19:02:07 CEST 2018


On Thu, 19 Jul 2018, Rich Shepard wrote:

> Since then I reformatted the file to two fields: date-time and elevation.
> If anyone wants a copy send me a message off the list and I'll respond with
> the modified file attached.

   This is a mistake. The file needs commas separating each field.

   I have the date and elev columns converted from factors to date and
numeric, respectively, but still have not learned how to convert the time.

   The source data file (head):
date,time,elev
2017-10-01,00:00,290.298
2017-10-01,00:30,290.301
2017-10-01,01:00,290.304
2017-10-01,01:30,290.295
2017-10-01,02:00,290.292
2017-10-01,02:30,290.289
2017-10-01,03:00,290.289
2017-10-01,03:30,290.289
2017-10-01,04:00,290.28

   These commands read the file and convert the date and elev columns:
wy2018 <- read.table('sh-2018.dat', header = T, sep = ',')
wy2018$date <- as.Date(as.character(wy2018$date, format='y-m-d'))
head(wy2018)
         date time    elev
1 2017-10-01   01 290.298
2 2017-10-01   01 290.301
3 2017-10-01   01 290.304
4 2017-10-01   01 290.295
5 2017-10-01   01 290.292
6 2017-10-01   01 290.289

   My attempts using chron() for the time column keep failing; e.g., 
wy2018$time <- chron(wy2018$time, format='h:m')
str(wy2018)
'data.frame':	12592 obs. of  3 variables:
  $ date: Date, format: "2017-10-01" "2017-10-01" ...
  $ time: 'dates' num  01 01 01 01 01 01 01 01 01 01 ...
   ..- attr(*, "format")= chr "h:m"
   ..- attr(*, "origin")= Named num  1 1 1970
   .. ..- attr(*, "names")= chr  "month" "day" "year"
  $ elev: num  290 290 290 290 290 ...

   Also, when I tried to use chron() for both the date and time columns of
the dataframe these failed, too.

   Please teach me how to read the data sources and produce dataframe columns
of date, time, and numeric.

Rich




More information about the R-help mailing list