【发布时间】:2017-01-11 09:31:14
【问题描述】:
我想根据另一列中的部分字符串创建一列。
参考栏遵循一般格式:GB/Ling 31st Dec
我想在这种情况下提取“Ling”这个词,它有不同的长度。
到目前为止,我的方法是:
library(data.table)
d1 <- data.table(MENU_HINT =
c("GB / Ling 31st Dec", "GB / Taun 30th Dec",
"GB / Ayr 19th Dec", "GB / Ayr 9th Nov",
"GB / ChelmC 29th Sep"),
Track = c("Ling", "Taun", "Ayr", "Ayr", "ChelmC"))
#remove all the spaces
d1[, Track2 := gsub("[[:space:]]", "", MENU_HINT)]
# get the position of the first digit
d1[, x := as.numeric(regexpr("[[:digit:]]", Track2)[[1]])]
# get the position of the '/'
d1[, y := as.numeric(regexpr("/", Track2))[[1]]]
# use above to extract the Track
d1[, Track2 := substr(Track2, y + 1, x - 1)]
Track 是我期望得到的,Track2 是我从上面的代码中得到的。
这看起来很啰嗦,而且似乎也不起作用,因为 x 和 y 值在整个列中都是相同的。
【问题讨论】:
-
请展示一个可重现的小例子和预期的输出
-
查看
str_extract包stringr的函数。 -
@akrun 道歉,小例子现已添加。
-
我不会为此使用正则表达式 - 它对于大数据集不会有效。您要查找的单词似乎始终位于第二个空格之后。一个非常简单有效的解决方案可能是
d1[, Track2 := tstrsplit(MENU_HINT, " ", fixed = TRUE)[[3]]] -
@DavidArenburg 感谢大卫,您的回答实际上比我的约 70 万行数据快 2.5 倍。
标签: r regex data.table