从新列中的R中的文本拆分日期和时间

时间:2017-05-11 12:32:43

标签: r

我的数据格式为

如何在第1列和第2列中的其他文本中记录日期和时间?请帮忙

10/05/2017, 17:50 - Messages you send to this group are now secured with end-to-end encryption. Tap for more info.


10/05/2017, 17:35 - ‪+91 85869 81933‬ created group “Shubhaaram Zone Northâ€

10/05/2017, 17:50 - Sang added you

10/05/2017, 17:51 - Sang added Soumit Deb

10/05/2017, 17:51 - Sang added ‪+91 98180 09003‬

10/05/2017, 17:53 - Sang added Astha Mckinsey

10/05/2017, 17:55 - Sang added ‪+91 96505 77422‬

10/05/2017, 17:58 - Sang added ‪+91 99106 61584‬

10/05/2017, 17:58 - Sang added ‪+91 96542 00700‬

10/05/2017, 17:59 - Sang added ‪+91 88722 03311‬

10/05/2017, 18:01 - Sang added Alok

2 个答案:

答案 0 :(得分:0)

示例data.frame:

df<- data.frame(col1= c("10/05/2017, 17:50 - Sang added you","10/05/2017, 17:51 - Sang added Soumit Deb"))

使用strsplit:

df$datetime<-unlist(lapply(as.character(df$col1), function(x) strsplit(x, split="-")[[1]][1]))
df$rest<-unlist(lapply(as.character(df$col1), function(x) strsplit(x, split="-")[[1]][2]))

答案 1 :(得分:0)

您可以使用stringr的str_split_fixed函数,假设df为:

df <- data.frame(col = c("10/05/2017, 17:50 - Messages you send to this group are now secured with end-to-end encryption. Tap for more info.",
                         "10/05/2017, 17:35 - ‪+91 85869 81933‬ created group “Shubhaaram Zone Northâ€",
                         "10/05/2017, 17:50 - Sang added you",
                         "10/05/2017, 17:51 - Sang added Soumit Deb",
                         "10/05/2017, 17:51 - Sang added ‪+91 98180 09003‬",
                         "10/05/2017, 17:53 - Sang added Astha Mckinsey",
                         "10/05/2017, 17:55 - Sang added ‪+91 96505 77422‬",
                         "10/05/2017, 17:58 - Sang added ‪+91 99106 61584‬",
                         "10/05/2017, 17:58 - Sang added ‪+91 96542 00700‬",
                         "10/05/2017, 17:59 - Sang added ‪+91 88722 03311‬",
                         "10/05/2017, 18:01 - Sang added Alok"))

<强>解决方案: 这里df1是你的最终解决方案

df1 <- setNames(data.frame(stringr::str_split_fixed(df$col,"-",n=2)),c("Datetime","Message"))