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

Rich Shepard r@hep@rd @ending from @ppl-eco@y@@com
Wed Jul 18 23:50:46 CEST 2018


   A set of data files have this format:

date,time,elev
1988-10-01,00:30,87.6849
1988-10-01,01:00,87.6849
1988-10-01,01:30,87.6849
1988-10-01,02:00,87.6879
1988-10-01,02:30,87.6879
1988-10-01,03:00,87.691
1988-10-01,03:30,87.694

Importing it with this command:

allyears <- read.table('allyears.dat', header = T, sep = ',')

produces this structure:

str(allyears)
'data.frame':	402414 obs. of  3 variables:
  $ date: Factor w/ 10230 levels "'data'","'date'",..:
  $ time: Factor w/ 1441 levels "'time'","00:00",..:
  $ elev: Factor w/ 4494 levels "'elev'","-3762938880000000369098752",..:

   Applying,
allyears$date <- as.Date(as.character(allyears$date))
changes the structure to
str(allyears)
'data.frame':	402414 obs. of  3 variables:
  $ date: Date, format: "1988-10-01" "1988-10-01" ...
  $ time: Factor w/ 1441 levels "'time'","00:00",..:
  $ elev: Factor w/ 4494 levels "'elev'","-3762938880000000369098752",..:

   I've not found the proper syntax to change time to a POSIXct time format
nor the elev to a numeric format.

allyears$time <- as.POSIXct(as.character(allyears$time, format=%H:%M))
Error: unexpected SPECIAL in "allyears$time <- as.POSIXct(as.character(allyears$time, format=%H:%"

and

allyears$elev <- as.Numeric(allyears$elev)
Error in as.Numeric(allyears$elev) : could not find function "as.Numeric"

   I've read ?read.table and looked on the web but I'm not finding how to
properly change the time and elev factors to H:M and fractional feet. A
pointer to a resource is much appreciated.

Rich



More information about the R-help mailing list