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

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Thu Jul 19 22:10:38 CEST 2018


> On Jul 19, 2018, at 10:02 AM, Rich Shepard <rshepard using appl-ecosys.com> wrote:
> 
> 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

I took the code that I offered earlier and replaced allyears with wy2018:

> txt <- "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"
> wy2018 <- read.table(text=txt, header = T, sep = ',')

> wy2018 $myDate <- as.Date(as.character(wy2018 $date))
> wy2018 $myTime <- as.POSIXct(paste(wy2018 $date, wy2018 $time))
> wy2018
        date  time    elev     myDate              myTime
1 2017-10-01 00:00 290.298 2017-10-01 2017-10-01 00:00:00
2 2017-10-01 00:30 290.301 2017-10-01 2017-10-01 00:30:00
3 2017-10-01 01:00 290.304 2017-10-01 2017-10-01 01:00:00
4 2017-10-01 01:30 290.295 2017-10-01 2017-10-01 01:30:00
5 2017-10-01 02:00 290.292 2017-10-01 2017-10-01 02:00:00
6 2017-10-01 02:30 290.289 2017-10-01 2017-10-01 02:30:00
7 2017-10-01 03:00 290.289 2017-10-01 2017-10-01 03:00:00
8 2017-10-01 03:30 290.289 2017-10-01 2017-10-01 03:30:00
9 2017-10-01 04:00 290.280 2017-10-01 2017-10-01 04:00:00

> str(wy2018)
'data.frame':	9 obs. of  5 variables:
 $ date  : Factor w/ 1 level "2017-10-01": 1 1 1 1 1 1 1 1 1
 $ time  : Factor w/ 9 levels "00:00","00:30",..: 1 2 3 4 5 6 7 8 9
 $ elev  : num  290 290 290 290 290 ...
 $ myDate: Date, format: "2017-10-01" "2017-10-01" ...
 $ myTime: POSIXct, format: "2017-10-01 00:00:00" "2017-10-01 00:30:00" ...

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