I new in R programming language. I have some time data time [1] "2005-01-03 05:11:39 UTC" "2005-01-03 06:36:02 UTC" "2005-01-03 06:36:55 UTC" [4] "2005-01-03 06:37:00 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03 06:38:04 UTC" [7] "2005-01-03 06:38:04 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03 06:38:05 UTC" [10] "2005-01-03 06:38:05 UTC" "2005-01-03 06:38:05 UTC" "2005-01-03 06:38:05 UTC" from this data set I would like to separate Year Month Day Hour Minute Second could you please tell which command can separate this data to the format which I needed? -- View this message in context: http://r.789695.n4.nabble.com/conversion-of-string-tp4320127p4320127.html Sent from the R help mailing list archive at Nabble.com.
Le lundi 23 janvier 2012 ? 00:35 -0800, uday a ?crit :> I new in R programming language. > > I have some time data > > time > [1] "2005-01-03 05:11:39 UTC" "2005-01-03 06:36:02 UTC" "2005-01-03 > 06:36:55 UTC" > [4] "2005-01-03 06:37:00 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03 > 06:38:04 UTC" > [7] "2005-01-03 06:38:04 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03 > 06:38:05 UTC" > [10] "2005-01-03 06:38:05 UTC" "2005-01-03 06:38:05 UTC" "2005-01-03 > 06:38:05 UTC" > > from this data set I would like to separate Year Month Day Hour Minute > Second > > could you please tell which command can separate this data to the format > which I needed?You should convert these to Date objects (if not done already), and then extract the relevant part using format.Date(). For example: strings <- c("2005-01-03 05:11:39 UTC", "2005-01-03 06:36:02 UTC", "2005-01-03 06:36:55 UTC") dates <- as.Date(strings) format.Date(dates, "%Y") to get the year. Cheers
Hi> > I new in R programming language. > > I have some time data > > time > [1] "2005-01-03 05:11:39 UTC" "2005-01-03 06:36:02 UTC" "2005-01-03 > 06:36:55 UTC" > [4] "2005-01-03 06:37:00 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03 > 06:38:04 UTC" > [7] "2005-01-03 06:38:04 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03 > 06:38:05 UTC" > [10] "2005-01-03 06:38:05 UTC" "2005-01-03 06:38:05 UTC" "2005-01-03 > 06:38:05 UTC" > > from this data set I would like to separate Year Month Day Hour Minute > Second > > could you please tell which command can separate this data to the format > which I needed?Convert it to POSIX or chron and use formating means for time data see e.g. ?strptime ?format Regards Petr> > > > -- > View this message in context:http://r.789695.n4.nabble.com/conversion-of-> string-tp4320127p4320127.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On 01/23/2012 07:35 PM, uday wrote:> I new in R programming language. > > I have some time data > > time > [1] "2005-01-03 05:11:39 UTC" "2005-01-03 06:36:02 UTC" "2005-01-03 > 06:36:55 UTC" > [4] "2005-01-03 06:37:00 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03 > 06:38:04 UTC" > [7] "2005-01-03 06:38:04 UTC" "2005-01-03 06:38:04 UTC" "2005-01-03 > 06:38:05 UTC" > [10] "2005-01-03 06:38:05 UTC" "2005-01-03 06:38:05 UTC" "2005-01-03 > 06:38:05 UTC" > > from this data set I would like to separate Year Month Day Hour Minute > Second > > could you please tell which command can separate this data to the format > which I needed? >Hi uday, If I read your message correctly, you don't have to convert the strings to dates. Try this: timebits<-list() timeseps<-c("-",":") for(i in 1:2) { for(j in 1:3) timebits[[3*(i-1)+j]]<-sapply(strsplit(sapply(strsplit( timestrings," "),"[",i),timeseps[i]),"[",j) } names(timebits)<- c("Year","Month","Day","Hour","Minute","Second") timebits Jim