我希望data.table
每个分组变量中的每个数字都有一列。假设我有以下数据:
Data <- data.table(Text = c("123 456", "234 567"), Group = c("A", "B"))
Text Group
1: 123 456 A
2: 234 567 B
我想要的是:
Text Group Number1 Number2
1: 123 456 A 123 456
2: 234 567 B 234 567
我尝试过的
Data[, c("Number1", "Number2") := str_extract_all(Text, "\\d+"), Group]
这给了我以下内容:
Error in `[.data.table`(Data, , `:=`(c("Number1", "Number2"), str_extract_all(Text, :
Supplied 2 items to be assigned to group 1 of size 1 in column 'Number1'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.
我知道我可以使用separate
,我知道我可以在每个数字/列中使用一个正则表达式,但是我想在的“一行”中找到一个聪明的解决方案str_extract_all
。
我会使用cSplit
,因为它是
splitstackshape::cSplit(Data, "Text", sep = " ", drop = FALSE)
但是,如果您想使用str_extract_all
我们可以
Data[,paste0("number", 1:2) := purrr::transpose(stringr::str_extract_all(Text, "\\d+"))]
Data
# Text Group number1 number2
#1: 123 456 A 123 456
#2: 234 567 B 234 567
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句