Reputation: 2384
I have a dataset that has the date and time split into multiple columns, I was looking to put them all into one column to plot. any help would be greatly appreciated, so far my data looks like this...
YY MM DD HH MM
2012 03 04 03 50
2012 03 04 03 40
2012 03 04 03 30
2012 03 04 03 20
2012 03 04 03 10
and would like it to look like this....
DateTime
2012-03-04 03:50
2012-03-04 03:40
2012-03-04 03:30
2012-03-04 03:20
2012-03-04 03:10
thanks in advance.
Upvotes: 1
Views: 107
Reputation: 3710
Another approach using paste
.
text1 <- "YY MM DD HH MM
2012 03 04 03 50
2012 03 04 03 40
2012 03 04 03 30
2012 03 04 03 20
2012 03 04 03 10"
d1 <- read.table(text=text1, head=T, as.is=T, colClasses = rep("character", 5))
library(dplyr)
d1 %>% transmute(DateTime=paste(paste(YY, MM, DD, sep="-"),
paste(HH, MM.1, sep=":")))
Upvotes: 0
Reputation: 886968
We can use sprintf
df1$DateTime <-do.call(sprintf, c(df1,
list(fmt = '%04d-%02d-%02d %02d:%02d')))
df1$datetime
#[1] "2012-03-04 03:50" "2012-03-04 03:40" "2012-03-04 03:30" "2012-03-04 03:20" "2012-03-04 03:10"
Upvotes: 4