Hello all, Let say I have following (numeric) vector: > x [1] 11.00 11.25 11.35 12.01 11.14 13.00 13.25 13.35 14.01 13.14 14.50 14.75 14.85 15.51 14.64 Now, I want to create a 'Date' variable (i.e. I should be able to do all calculations pertaining to date/time and also time-series plotting etc.) like 2012-12-31 11:00:00 AM, 2012-12-31 11:25:00 AM, 2012-12-31 11:35:00 AM, 2012-12-31 12:01:00 PM, . . . . Is it possible to achieve that? Thanks and regards,
Hi, Try this: x<-c(11.00,11.25,11.35,12.01,11.14,13.00,13.25,13.35,14.01,13.14,14.50,14.75,14.85,15.51,14.64) ?x[substr(x,4,5)>=60]<-(x[substr(x,4,5)>=60]-.60)+1 ?res<-sort(as.POSIXct(paste("2012-12-31", sprintf("%.2f",x),sep=" "),format="%Y-%m-%d %H.%M")) # ?ifelse(format(res,"%H:%M")>=12, paste(res,"PM"), paste(res,"AM")) # [1] "2012-12-31 11:00:00 AM" "2012-12-31 11:14:00 AM" "2012-12-31 11:25:00 AM" ?#[4] "2012-12-31 11:35:00 AM" "2012-12-31 12:01:00 PM" "2012-12-31 13:00:00 PM" ?#[7] "2012-12-31 13:14:00 PM" "2012-12-31 13:25:00 PM" "2012-12-31 13:35:00 PM" #[10] "2012-12-31 14:01:00 PM" "2012-12-31 14:50:00 PM" "2012-12-31 15:04:00 PM" #[13] "2012-12-31 15:15:00 PM" "2012-12-31 15:25:00 PM" "2012-12-31 15:51:00 PM" str(ifelse(format(res,"%H:%M")>=12, paste(res,"PM"), paste(res,"AM"))) ?#chr [1:15] "2012-12-31 11:00:00 AM" "2012-12-31 11:14:00 AM" ... str(res) # POSIXct[1:15], format: "2012-12-31 11:00:00" "2012-12-31 11:14:00" ... ? A.K. ----- Original Message ----- From: Christofer Bogaso <bogaso.christofer at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, December 31, 2012 12:12 PM Subject: [R] Question on creating Date variable Hello all, Let say I have following (numeric) vector:> x[1] 11.00 11.25 11.35 12.01 11.14 13.00 13.25 13.35 14.01 13.14 14.50 14.75 14.85 15.51 14.64 Now, I want to create a 'Date' variable (i.e. I should be able to do all calculations pertaining to date/time and also time-series plotting etc.) like 2012-12-31 11:00:00 AM, 2012-12-31 11:25:00 AM, 2012-12-31 11:35:00 AM, 2012-12-31 12:01:00 PM, . . . . Is it possible to achieve that? Thanks and regards, ______________________________________________ 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.
Hello, Try the following. x <- scan(text = "11.00 11.25 11.35 12.01 11.14 13.00 13.25 13.35 14.01 13.14 14.50 14.75 14.85 15.51 14.64 ") hours <- floor(x) mins <- (100*x) %% 100 as.POSIXct(paste(Sys.Date(), hours, mins), format = "%Y-%m-%d %H %M") As you can see, there are three values of 'x' with decimal part greater than 60, which will give mins > 60 and therefore NA date/times. Hope this helps, Rui Barradas Em 31-12-2012 17:12, Christofer Bogaso escreveu:> Hello all, > > Let say I have following (numeric) vector: > > > x > [1] 11.00 11.25 11.35 12.01 11.14 13.00 13.25 13.35 14.01 13.14 14.50 > 14.75 14.85 15.51 14.64 > > Now, I want to create a 'Date' variable (i.e. I should be able to do > all calculations pertaining to date/time and also time-series plotting > etc.) like > > 2012-12-31 11:00:00 AM, 2012-12-31 11:25:00 AM, 2012-12-31 11:35:00 > AM, 2012-12-31 12:01:00 PM, . . . . > > > Is it possible to achieve that? > > Thanks and regards, > > ______________________________________________ > 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.
On Dec 31, 2012, at 9:12 AM, Christofer Bogaso wrote:> Hello all, > > Let say I have following (numeric) vector: > > > x > [1] 11.00 11.25 11.35 12.01 11.14 13.00 13.25 13.35 14.01 13.14 > 14.50 14.75 14.85 15.51 14.64 > > Now, I want to create a 'Date' variable (i.e. I should be able to do > all calculations pertaining to date/time and also time-series > plotting etc.) like > > 2012-12-31 11:00:00 AM, 2012-12-31 11:25:00 AM, 2012-12-31 11:35:00 > AM, 2012-12-31 12:01:00 PM, . . . . >Those _times_ ( _not_ Dates) cannot possibly be in %M.%S" format, given the number of items to the right of the decimal point that are greater than 60. So will proceed on the arguably more likely assumption that they are in fractional minutes. To recover from that problem, one might consider: > as.POSIXct(paste( floor(x), round(60*(x-floor(x))) ), format="%M %S") [1] "2012-12-31 00:11:00 PST" "2012-12-31 00:11:15 PST" [3] "2012-12-31 00:11:21 PST" "2012-12-31 00:12:01 PST" [5] "2012-12-31 00:11:08 PST" "2012-12-31 00:13:00 PST" [7] "2012-12-31 00:13:15 PST" "2012-12-31 00:13:21 PST" [9] "2012-12-31 00:14:01 PST" "2012-12-31 00:13:08 PST" [11] "2012-12-31 00:14:30 PST" "2012-12-31 00:14:45 PST" [13] "2012-12-31 00:14:51 PST" "2012-12-31 00:15:31 PST" [15] "2012-12-31 00:14:38 PST" -- David Winsemius, MD Alameda, CA, USA