Hello,

Tam Le wrote:
Hello,

Please excuse me if this question has been asked before. I'm new to R, and
have been trying to google the answers without any success.

I would like to convert a set of date and time into R date-time class. Right
now, the dates and times are in integer format, so I first need to convert
them into string, and then to R date-time using strptime. However, I have a
problem converting them from integer to string:

data:

Date Time
20091209 1200
20091209 1500
20091209 1800
....
20091210 800
20091210 1000
20091210 1600
....

I used:

tempdate=toString(Date)
day=substr(tempdate, 7, 8)
month=substr(tempdate, 5, 6)
year=substr(tempdate, 1, 2)
rDate=strptime(paste(year, month, day, sep="-"))

the 1st command does not create a new column with string dates in it, it
creates a single row that has all the dates in a single string, like this:
"20091209, 20091209, .... 20091210, 20091210"
"day" ends up being 1 number, 09, instead of a column of days. I ended up
having all rDate=2009/12/09, which is the first row.

Could you please tell me how to do this correctly?

#sample data.frame
tmp <- data.frame(Date = c("20091209", "20091209"), Time = c("1200", "1500"))

#Convert, see ?strptime and ?as.POSIXct format argument
as.POSIXct(paste(tmp$Date, tmp$Time), format = "%Y%m%d %H%M")

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to