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

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Thu Jul 19 00:17:11 CEST 2018


> On Jul 18, 2018, at 2:50 PM, Rich Shepard <rshepard using appl-ecosys.com> wrote:
> 
>  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.

I would not destroy the possibility of using the original values:

> allyears$myDate <- as.Date(as.character(allyears$date))
> allyears$myTime <- as.POSIXct(paste(allyears$date, allyears$time))
> allyears
        date  time    elev     myDate              myTime
1 1988-10-01 00:30 87.6849 1988-10-01 1988-10-01 00:30:00
2 1988-10-01 01:00 87.6849 1988-10-01 1988-10-01 01:00:00
3 1988-10-01 01:30 87.6849 1988-10-01 1988-10-01 01:30:00
4 1988-10-01 02:00 87.6879 1988-10-01 1988-10-01 02:00:00
5 1988-10-01 02:30 87.6879 1988-10-01 1988-10-01 02:30:00
6 1988-10-01 03:00  87.691 1988-10-01 1988-10-01 03:00:00
7 1988-10-01 03:30 87.694t 1988-10-01 1988-10-01 03:30:00

> 


> 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"

It's spelled `as.numeric`, but I'm having difficulty thinking about what sort of elevation (or would that be "depth") would be be measured by "-3762938880000000369098752".


> 
>  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
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law




More information about the R-help mailing list