我想获取列表列中每个向量中的前 5 个值,并将其作为保存为列表的数据框中的新列返回。
structure(list(sample_num = 1:6, vector = list(c(0, 1, 1, 0,
1, 2, 0, 0, 3, 0), c(0, 0, 1, 2, 0, 0, 4, 10, 12, 1), c(1, 33,
4, 4, 2, 2, 6, 9, 14, 2), c(0, 0, 1, 0, 1, 0, 1, 5, 3, 0), c(0,
1, 1, 0, 0, 0, 1, 4, 3, 0), c(0, 0, 1, 0, 0, 0, 1, 1, 1, 0))), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -6L), .Names = c("sample_num",
"vector"))
> test
# A tibble: 6 × 2
sample_num vector
<int> <list>
1 1 <dbl [10]>
2 2 <dbl [10]>
3 3 <dbl [10]>
4 4 <dbl [10]>
5 5 <dbl [10]>
6 6 <dbl [10]>
我尝试使用 lmap 但收到错误消息
> test %>% lmap(.$vector,.f = function(x) x[1:5])
Error in .f(.x[i], ...) :
unused argument (list(c(0, 1, 1, 0, 1, 2, 0, 0, 3, 0), c(0, 0, 1, 2, 0, 0, 4, 10, 12, 1), c(1, 33, 4, 4, 2, 2, 6, 9, 14, 2), c(0, 0, 1, 0, 1, 0, 1, 5, 3, 0), c(0, 1, 1, 0, 0, 0, 1, 4, 3, 0), c(0, 0, 1, 0, 0, 0, 1, 1, 1, 0)))
谢谢!