我有一个数据表
x<-data.table(col1=rep("hello,how,are,you",4),col2=1:4)
col1 col2
1: hello,how,are,you 1
2: hello,how,are,you 2
3: hello,how,are,you 3
4: hello,how,are,you 4
现在我需要根据col2中的值从col1中选择值。我怎样才能在data.table中执行此操作?
最终数据看起来应该是这样的
col1 col2 col3
1: hello,how,are,you 1 hello
2: hello,how,are,you 2 how
3: hello,how,are,you 3 are
4: hello,how,are,you 4 you
答案 0 :(得分:4)
我不知道这是否是最有效的(速度或内存是最受关注的?)解决方案:
library(data.table)
x <- data.table(col1=c(rep("hello,how,are,you",3),
"Lirum, Larum, Löffel, Stiel"),
col2=1:4)
x[, ind := .I]
x[, col3 := strsplit(col1, ",", fixed=TRUE)]
x[, col4 := col3[[1]][col2], by=ind]
# col1 col2 ind col3 col4
# 1: hello,how,are,you 1 1 hello,how,are,you hello
# 2: hello,how,are,you 2 2 hello,how,are,you how
# 3: hello,how,are,you 3 3 hello,how,are,you are
# 4: Lirum, Larum, Löffel, Stiel 4 4 Lirum, Larum, Löffel, Stiel Stiel
答案 1 :(得分:3)
这是另一个选项,它将strsplit
的结果合并到一个矩阵中,然后使用矩阵子集:
x <- data.table(col1=rep("hello,how,are,you",4),col2=1:4)
x[, col3 := do.call(rbind, strsplit(col1, split = ",", fixed = TRUE))
[matrix(c(1:.N, col2), ncol = 2)]]
x
# col1 col2 col3
#1: hello,how,are,you 1 hello
#2: hello,how,are,you 2 how
#3: hello,how,are,you 3 are
#4: hello,how,are,you 4 you
答案 2 :(得分:1)
我希望以下可以为你效劳......
解决方案1:如果col1没有固定的字长
col1 = c(paste(c('hello','how','are','you'),1,sep='-',collapse=','),
paste(c('hello','how','are','you'),2,sep='-',collapse=','),
paste(c('hello','how','are','you'),3,sep='-',collapse=','),
paste(c('hello','how','are','you'),4,sep='-',collapse=','))
x<-data.table(col1=col1,col2=1:4)
x$col3 = NA
for(i in 1:nrow(x)){
x$col3[i] = strsplit(x$col1[i],',')[[1]][x$col2[i]]
}