> On Jul 19, 2018, at 10:02 AM, Rich Shepard <rshepard at 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.28I 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)) > wy2018date 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 at 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
Rich Shepard
2018-Jul-19 20:21 UTC
[R] Read in data table, change columns from factors [RESOLVED]
On Thu, 19 Jul 2018, David Winsemius wrote:> I took the code that I offered earlier and replaced allyears with 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" ...David, Thank you. I see the results in the dataframe structure although I still don't understand all the reasons. The 'myTime' column confirms what I thought: that there is no separate time data type. I'll use what you taught me an move on with the analyses. Best regards, Rich
David Winsemius
2018-Jul-19 21:01 UTC
[R] Read in data table, change columns from factors [RESOLVED]
> On Jul 19, 2018, at 1:21 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote: > > On Thu, 19 Jul 2018, David Winsemius wrote: > >> I took the code that I offered earlier and replaced allyears with 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" ... > > David, > > Thank you. I see the results in the dataframe structure although I still > don't understand all the reasons. The 'myTime' column confirms what I > thought: that there is no separate time data type. I'll use what you taught > me an move on with the analyses.You can use format to only display the time portion of a datetime object. format( Sys.time(), "%H:%M") [1] "13:57" You can append the current date to a "time-only" character value and as.POSIXct will do that for you: as.POSIXct("00:00", format="%H:%M") [1] "2018-07-19 PDT"> as.POSIXct(c("00:00", "00:01"), format="%H:%M")[1] "2018-07-19 00:00:00 PDT" "2018-07-19 00:01:00 PDT" There is a difftime-class in base R And the lubridate package defines a duration class. It's not a package I use, and I cannot tell off the top of my head what it thinks the difference might be between a "time-span" and a "duration". Best of luck.> > Best regards, > > Rich > > ______________________________________________ > R-help at 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