我想将7列数据框 icols 转换为整数向量的列表列。
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]>
...
答案 0 :(得分:1)
一个带有{% autoescape off %}{{ noticia.noticia_texto }}{% endautoescape %}
的选项是
tidyverse
如果'desired_column'应该是library(tidyverse)
icols %>%
mutate(desired_column = pmap(., ~ c(..., use.names = FALSE)))
# A tibble: 5 x 8
# CBT MI Educ Fam CM PeerGroup ICM desired_column
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <list>
#1 0 0 0 0 0 0 0 <dbl [7]>
#2 0 1 0 0 0 0 0 <dbl [7]>
#3 0 1 0 0 0 0 0 <dbl [7]>
#4 1 1 0 0 0 0 0 <dbl [7]>
#5 1 1 0 0 0 0 0 <dbl [7]>
类
integer
或者使用icols %>%
mutate(desired_column = pmap(., ~ c(..., use.names = FALSE) %>%
as.integer))
中的split
base R