【发布时间】:2019-09-17 14:32:34
【问题描述】:
我想将 7 列数据框 icls 转换为整数向量的列表列。
icols <- structure(list(CBT = c(0, 0, 0, 1, 1), MI = c(0, 1, 1, 1, 1),
Educ = c(0, 0, 0, 0, 0), Fam = c(0, 0, 0, 0, 0), CM = c(0,
0, 0, 0, 0), PeerGroup = c(0, 0, 0, 0, 0), ICM = c(0, 0,
0, 0, 0)), row.names = c(NA, -5L), class = c("tbl_df", "tbl",
"data.frame"))
下面的代码创建了一个字符串向量。
> do.call(paste, as.data.frame(icols))
[1] "0 0 0 0 0 0 0" "0 1 0 0 0 0 0" "0 1 0 0 0 0 0" "1 1 0 0 0 0 0" "1 1 0 0 0 0 0"
我想创建一个列表列,其中每个字符串的7个元素变成一个整数向量。
所需的输出类似于:
CBT MI Educ Fam CM PeerGroup ICM new_column
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <list>
1 0 0 0 0 0 0 0 <int [7]>
...
【问题讨论】:
-
你需要
split(icols, 1:nrow(icols)) -
如果你想作为矢量
split(unlist(icols, use.names = FALSE), row(icols))