[英]How can I make a list-column that is a subset of another list-column in R?
I have a data frame with one column as a list-column, ie it is a column that has, for each row, two vectors contained in that column.我有一个数据框,其中一列作为列表列,即它是一列,对于每一行,该列中包含两个向量。 I would like to be able to make another column in my data frame that is also a list-column, but that only contains a single sub-list (rather than two), and I would like that list to be the first three elements of one of the sub-lists of the column with two sub-lists.
我希望能够在我的数据框中创建另一个列,它也是一个列表列,但它只包含一个子列表(而不是两个),我希望该列表成为前三个元素具有两个子列表的列的子列表之一。
A simple reproducible example is provided below:下面提供了一个简单的可重现示例:
df <- data.frame(state = c(rep("Alabama", 5), rep("Alaska", 5), rep("Arizona", 5), rep("Arkansas", 5), rep("California", 5)),
letter = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"),
freq = c(8, 7, 4, 3, 1, 19, 15, 7, 4, 2, 10, 6, 3, 2, 2, 11, 10, 10, 5, 4, 50, 33, 22, 11, 1))
df <- nest(df, letter_list = c(letter, freq))
In the context of this reprex, I would like to have a third column in df
that has, for each state, a list of the first three elements of letter
(which is contained in letter_list
).在这个代表的上下文中,我想在
df
中有第三列,对于每个 state,都有一个letter
前三个元素的列表(包含在letter_list
中)。
I have attempted to use purrr functions, such as map()
, in conjunction with the head()
function to mutate
a new variable, but this has been unsuccessful;我曾尝试将 purrr 函数(例如
map()
与head()
function 结合使用来mutate
一个新变量,但这并不成功; my new column is populated with lists of length 0.我的新列填充了长度为 0 的列表。
If possible, a solution using the tidyverse
would be ideal.如果可能,使用
tidyverse
的解决方案将是理想的。
Any help would be greatly appreciated!任何帮助将不胜感激!
Use map
to loop over the list
column, select
the 'letter', get the first 3 with either Extract ( [
) or use slice_head
使用
map
循环遍历list
列select
'字母',使用 Extract ( [
) 或使用slice_head
获取前 3 个
library(dplyr)
library(purrr)
df %>%
mutate(letter_new = map(letter_list, ~
.x %>%
select(letter) %>%
slice_head(n = 3) %>%
pull(letter)))
-output -输出
# A tibble: 5 × 3
state letter_list letter_new
<chr> <list> <list>
1 Alabama <tibble [5 × 2]> <chr [3]>
2 Alaska <tibble [5 × 2]> <chr [3]>
3 Arizona <tibble [5 × 2]> <chr [3]>
4 Arkansas <tibble [5 × 2]> <chr [3]>
5 California <tibble [5 × 2]> <chr [3]>
NOTE: if it needs to be kept as tibble
, we don't need the last pull
step注意:如果它需要保留为
tibble
,我们不需要最后的pull
步骤
Or using base R
或使用
base R
df$letter_new <- lapply(df$letter_list, \(x) head(x$letter, 3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.