Splitting a factor at a space in R -
i want split x (which factor)
dd = data.frame(x = c("29-4-2014 06:00:00", "9-4-2014 12:00:00", "9-4-2014 00:00:00", "6-5-2014 00:00:00" ,"7-4-2014 00:00:00" , "29-5-2014 00:00:00")) x 29-4-2014 06:00:00 9-4-2014 12:00:00 9-4-2014 00:00:00 6-5-2014 00:00:00 7-4-2014 00:00:00 29-5-2014 00:00:00 at horizontal space , 2 columns as:
x.date x.time 29-4-2014 06:00:00 9-4-2014 12:00:00 9-4-2014 00:00:00 6-5-2014 00:00:00 7-4-2014 00:00:00 29-5-2014 00:00:00 any suggestion appreciated!
here approach using lubridate:
dd = data.frame(x = c("29-4-2014 06:00:00", "9-4-2014 12:00:00", "9-4-2014 00:00:00", "6-5-2014 00:00:00" ,"7-4-2014 00:00:00" , "29-5-2014 00:00:00"), stringsasfactors = false) note use of stringsasfactors = false, prevents dates being read factors.
library(lubridate) dd2 <- transform(dd,x2 = dmy_hms(x)) transform(dd2, the_year = year(x2)) x x2 the_year 1 29-4-2014 06:00:00 2014-04-29 06:00:00 2014 2 9-4-2014 12:00:00 2014-04-09 12:00:00 2014 3 9-4-2014 00:00:00 2014-04-09 00:00:00 2014 4 6-5-2014 00:00:00 2014-05-06 00:00:00 2014 5 7-4-2014 00:00:00 2014-04-07 00:00:00 2014 6 29-5-2014 00:00:00 2014-05-29 00:00:00 2014
Comments
Post a Comment