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? Thanks, Tam -- Tam T. Le [MEDS 2001 Sheridan Rd, Evanston IL 60208] [847 708 2439] [[alternative HTML version deleted]]
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")
Also, although you don't need it to accomplish what you want, you were probably looking for the as.character function instead of toString... 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? > > Thanks, > Tam > >
Gabor Grothendieck
2010-Feb-01 19:42 UTC
[R] Convert a column of numbers to a column of strings
Try this> Lines <- "Date Time Data+ 20091209 1200 1 + 20091209 1500 2 + 20091209 1800 3 + 20091210 800 4 + 20091210 1000 5 + 20091210 1600 6"> > toChron <- function(x) as.chron(sprintf("%d %04d", x[,1], x[,2]), "%Y%m%d %H%M") > > DF <- read.table(textConnection(Lines), header = TRUE) > toChron(DF)[1] (12/09/09 12:00:00) (12/09/09 15:00:00) (12/09/09 18:00:00) (12/10/09 08:00:00) (12/10/09 10:00:00) (12/10/09 16:00:00) Or if the read problem is to read in a time series try read.zoo in the zoo package. The development version of read.zoo supports multi-column indexes:> > library(zoo) > # grab development version of read.zoo > source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/zoo/R/read.zoo.R?rev=611&root=zoo") > z <- read.zoo(textConnection(Lines), header = TRUE, index = 1:2, FUN = toChron); z(12/09/09 12:00:00) (12/09/09 15:00:00) (12/09/09 18:00:00) (12/10/09 08:00:00) (12/10/09 10:00:00) (12/10/09 16:00:00) 1 2 3 4 5 6 For more info on dates and times see the article in R News 4/1. For more info on zoo see the 3 vignettes (pdf documents) that come with the package. On Mon, Feb 1, 2010 at 1:52 PM, Tam Le <lettam at gmail.com> 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? > > Thanks, > Tam > > > -- > > > Tam T. Le [MEDS 2001 Sheridan Rd, Evanston IL 60208] [847 708 2439] > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >