r - How do I combine two columns into one? -
i stuck combining 2 columns of data frame one. below code reproduce problem
df <- data.frame(date = c("2/13/1962 0:00:00", "4/13/1972 0:00:00", "3/13/1982 0:00:00", "12/13/1992 0:00:00"), time = c("0900", "1000", "1101", "1603")) #this works df$tmp <- as.date(df$date, format="%m/%d/%y") df date time tmp 1 2/13/1962 0:00:00 0900 1962-02-13 2 4/13/1972 0:00:00 1000 1972-04-13 3 3/13/1982 0:00:00 1101 1982-03-13 4 12/13/1992 0:00:00 1603 1992-12-13 # doesn't df$tmp <- strptime(strsplit(as.character(df$date), " ")[[1]][1], format="%m/%d/%y") df 1 2/13/1962 0:00:00 0900 1962-02-13 2 4/13/1972 0:00:00 1000 1962-02-13 3 3/13/1982 0:00:00 1101 1962-02-13 4 12/13/1992 0:00:00 1603 1962-02-13
i end column combines date part of date , time column unable result.
while experimenting noticed as.date
function convert date . on other hand, strsplit
function rows end first date.
i appreciate towards solution, ending 4 rows have "date" object:
1 1962-02-13 09:00 2 1972-04-13 10:00 3 1982-03-13 11:01 4 1992-12-13 16:03
you may paste
date , time variable, , use as.posixct
appropriate format
:
as.posixct(x = paste(as.date(df$date, format = "%m/%d/%y"), df$time), format = "%y-%m-%d %h%m")
Comments
Post a Comment