[英]How to count elements in each column of a list column in data frame in R
我有一个数据框df
,其中一列是列表列。 dataframe 中此列的每个元素都是列表。 那么如何在df
中包含(变异)一个新列,为我提供该列表列中相应元素的计数。
样本数据
df <- structure(list(x1 = 1:5,
x2 = list(c("a", "b"), c("b", "c", "d"), c("a", "b"), c("a", "b"), c("a", "b"))),
row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))
> df
# A tibble: 5 x 2
x1 x2
<int> <list>
1 1 <chr [2]>
2 2 <chr [3]>
3 3 <chr [2]>
4 4 <chr [2]>
5 5 <chr [2]>
我试过这个但无济于事
df %>% mutate(x3 = length(x2[[row_number()]]))
Error: Problem with `mutate()` input `x3`.
x recursive indexing failed at level 2
i Input `x3` is `length(x2[[row_number()]])`.
Run `rlang::last_error()` to see where the error occurred.
我想要的输出是这样的
# A tibble: 5 x 3
x1 x2 x3
<int> <list> <int>
1 1 <chr [2]> 2
2 2 <chr [3]> 3
3 3 <chr [2]> 2
4 4 <chr [2]> 2
5 5 <chr [2]> 2
尝试lengths
,它计算每行中的元素数。
df$x3 <- lengths(df$x2)
df
# x1 x2 x3
# 1 1 a, b 2
# 2 2 b, c, d 3
# 3 3 a, b 2
# 4 4 a, b 2
# 5 5 a, b 2
一个 tidyverse 替代方法是使用rowwise
:
df %>% rowwise() %>% mutate(x3 = length(x2))
# A tibble: 5 x 3
# Rowwise:
x1 x2 x3
<int> <list> <int>
1 1 <chr [2]> 2
2 2 <chr [3]> 3
3 3 <chr [2]> 2
4 4 <chr [2]> 2
5 5 <chr [2]> 2
使用map
library(dplyr)
library(purrr)
df %>%
mutate(x3 = map_int(x2, length))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.