0

从 data.frame 开始,例如:

df = read.table(text = "ref1  code1,code2
           ref2 code3,code4,code5
           ref3 code6", stringsAsFactors=F)
names(df) = c('id', 'codes')
print(df)
    id             codes
1 ref1       code1,code2
2 ref2 code3,code4,code5
3 ref3             code6

希望得到这样的结果:

lst = list()
for(i in 1:3) lst[[df[i,1]]] = strsplit(df[i,2], ',')[[1]]
print(lst)
$ref1
[1] "code1" "code2"

$ref2
[1] "code3" "code4" "code5"

$ref3
[1] "code6"

如果没有(慢)迭代,怎么可能达到这一点? as.list(df)仅按列工作:

$id
[1] "ref1" "ref2" "ref3"

$codes
[1] "code1,code2"       "code3,code4,code5" "code6" 

提前致谢。

4

3 回答 3

3

像这样的东西,也许:

lapply(split(df$codes,df$id),function(x) strsplit(x,split = ",")[[1]])
$ref1
[1] "code1" "code2"

$ref2
[1] "code3" "code4" "code5"

$ref3
[1] "code6"

下面提到的阿南达的解决方案是恕我直言,要优越得多:

setNames(strsplit(df$codes, ","), df$id)
于 2013-10-04T15:39:05.463 回答
2

这是另一种方法。

> lst <- unlist(apply(df[,2, drop=FALSE], 1, strsplit, ","), recursive=FALSE)
> names(lst) <- df[,1]
$ref1
[1] "code1" "code2"

$ref2
[1] "code3" "code4" "code5"

$ref3
[1] "code6"

setNames用于命名列表,如@Henrik's answer

> setNames(unlist(apply(df[,2, drop=FALSE], 1, strsplit, ","), recursive=FALSE), df$id)
于 2013-10-04T15:40:45.993 回答
2

你也可以试试这个

library(splitstackshape)
ll <- concat.split.list(data = df,
                        split.col = "codes",                
                        drop = TRUE)[[2]]
names(ll) <- df$id
ll

# $ref1
# [1] "code1" "code2"
# 
# $ref2
# [1] "code3" "code4" "code5"
# 
# $ref3
# [1] "code6

在@Ananda Mahto 的评论之后更新。谢谢!

setNames(concat.split.list(df, "codes")[["codes_list"]], df$id)
于 2013-10-04T16:01:45.063 回答