Welcome, Guest
Username: Password: Remember me

TOPIC: Dates/Times

Dates/Times 8 months 2 weeks ago #40

  • DanR
  • Offline
  • New Member
  • Posts: 2
  • Karma: 0
After the morning R session at the NERRA meeting, I put some Googling time into finding out how to tell R how to handle date/time columns when reading from a file. It turns out to be relatively straight forward, here is a quick snippet:
#Here we create our own Date type, SWMPRDate and define the function
#to handle it.
setAs("character","SWMPRDate", function(from) strptime(from, format="%m/%d/%Y") )
#Here we call the read.csv, but with some extra parameters, namely the #colClasses. colClasses is where we specify the data type of each column.
#Take note the SWMPRDate column type. When R encounters it, it will use
#the function defined for is.
WQBData <- read.csv("data_WQBCR.csv", 
                        header=TRUE,
                        sep=",",
                        colClasses=c("SWMPRDate",  
                                     "numeric", "numeric","numeric","numeric","numeric","numeric"
                        ))

Let's say we had a date time since the sites sample at 15 minute intervals. We could define a type as so:
setAs("character","SWMPRDateTime", function(from) strptime(from, format="%m/%d/%Y %H:%M:%S") )

You need to verify what format the date/time data you are working with. In my examples above I used the sample that the example data files were in which is a month/day/4 digit Year.
The "%" directive definitions for the strptime function can be found here

Dan
Last Edit: 8 months 2 weeks ago by DanR.
The administrator has disabled public write access.

Dates/Times 8 months 2 weeks ago #41

  • Marcus Beck
  • Offline
  • Administrator
  • Posts: 32
  • Thank you received: 5
  • Karma: 4
Excellent, thanks for the helpful code, Dan. I've never seen that 'setAs' function. I wonder though if you might run into problems if there are other character columns in the data that aren't meant to be 'SWMPRDate' objects.

I also thought I'd mention another helpful package: readxl. There have been attempts in the past by other packages to import excel files directly into R but this one is the first to do it well, in my opinion. The author is Hadley Wickham, author of many other packages including ggplot2 and dplyr. He's also the chief data scientist at RStudio. Anyhow, this package seems to do a good job of finding date columns and identifying their structure so they are converted correctly on import. I'm still stuck in the flat file world of text files so I haven't used it much, but I generally trust his packages to do what they're meant to do.

Hope that helps.

-Marcus
The administrator has disabled public write access.

Dates/Times 8 months 2 weeks ago #42

  • DanR
  • Offline
  • New Member
  • Posts: 2
  • Karma: 0
Glad to be able to contribute. I have a dataset where I have a couple of regular 'character' columns, and the setAs did not affect them. I suspect what it's doing is making the 'SWMPRDate' a character type for the call into the strptime function. Although don't quote me on that.
The administrator has disabled public write access.
Time to create page: 0.109 seconds
Powered by Kunena Forum