繁体   English   中英

如何从R中的字符串中提取特定单词?

[英]how to extract the specific words from a string in R?

如何提取“7-9”、“2-5”和“2-8”,然后作为 event_time 粘贴到新列?
事件细节
2.9(S) 7-9街【火车】#2097
2.1(S) 2-5街【火车】#2012
2.2(S) 2-8A TBC【列车】#202

您还没有真正分享提取数字的逻辑,但根据您分享的有限数据,我们可以:

df$new_col <- sub('.*(\\d+-\\d+).*', '\\1', df$event_details)

df
#               event_details new_col
#1 2.9(S) 7-9 street【Train】     7-9
#2 2.1(S) 2-5 street【Train】     2-5
#3   2.2(S) 2-8A TBC【Train】     2-8

或者同样使用str_extract

df$new_col <- stringr::str_extract(df$event_details, "\\d+-\\d+")

数据

df <- structure(list(event_details = structure(c(3L, 1L, 2L), 
.Label = c("2.1(S) 2-5 street【Train】", 
"2.2(S) 2-8A TBC【Train】", "2.9(S) 7-9 street【Train】"), class = 
"factor")), class = "data.frame", row.names = c(NA, -3L))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM