1
我想用特定格式(前面的最小示例)转换列表中的数据帧。 对于下面的数据框,我想最后列出5个元素,每个元素都有一个由5个元素组成的列表。将data.frame/tibble以分组方式转换为列表
parameters = tidyr::expand(tibble(id=1:5, value = 1:5 * 2),id,value)
# A tibble: 6 x 2
id value
<int> <dbl>
1 1 2
2 1 4
3 1 6
4 1 8
5 1 10
6 2 2
result = base::split(parameters, list(parameters$id))
result = lapply(result, function(x) { x["id"] <- NULL; x })
到目前为止,它获得了id权(5个元素),但不是它们中的子元素。它给出了第一个元素
> a[1]
$`1`
# A tibble: 5 x 1
value
<dbl>
1 2
2 4
3 6
4 8
5 10
,而我希望把它作为
> as.list(a$`1`$value)
[[1]]
[1] 2
[[2]]
[1] 4
[[3]]
[1] 6
[[4]]
[1] 8
[[5]]
[1] 10
这样的结果看起来是这样的
[1]
[[1]]
[1] 2
[[2]]
[1] 4
[[3]]
[1] 6
[[4]]
[1] 8
[[5]]
[1] 10
[2]
[[1]]
[1] 2
[[2]]
[1] 4
[[3]]
[1] 6
[[4]]
[1] 8
[[5]]
[1] 10
是否有任何优雅的方式为了达成这个?
注意,'结果=基地::分(参数$值,列表(参数的$ id))'使第二'lapply'线不必要。 – lmo
感谢您的提示! – MaHo