如何使用dplyr在一列中总结组的唯一值?

发布于 2025-01-26 06:58:32 字数 1049 浏览 4 评论 0原文

目前,我有以下代码:

categories <- df %>%                               #this is a very large df but that should not matter to my question
  group_by(category, subcategory, IV_type) %>%
  summarise(n = n())

生成以下DF:

category <- c('a','a','a','a','b','b','b','c','c')
subcategory <- c(1,1,2,3,4,4,5,6,7)
N <- c(21,13,7,9,11,17,19,23,27)
type <- c('nom', 'ord', 'nom', 'scale', 'nom', 'scale', 'nom', 'scale', 'scale')

categories <- data.frame(category, subcategory, N, type)

但是,我想获得此数据框架:

category1 <- c('a','a','a','b','b','c','c')
subcategory1 <- c(1,2,3,4,5,6,7)
N1 <- c(34,7,9,28,19,23,27)
type1 <- c('nom, ord', 'nom', 'scale', 'nom, scale', 'nom', 'scale', 'scale')

categories1 <- data.frame(category1, subcategory1, N1, type1)

我的尝试:

categories <- df %>%
  group_by(category, subcategory) %>%
  summarise(n = n(), unique_types = unique(type))

不幸的是,这会引发错误。有人知道我如何实现这一目标吗?

At the moment I have the following code:

categories <- df %>%                               #this is a very large df but that should not matter to my question
  group_by(category, subcategory, IV_type) %>%
  summarise(n = n())

Which produces the following df:

category <- c('a','a','a','a','b','b','b','c','c')
subcategory <- c(1,1,2,3,4,4,5,6,7)
N <- c(21,13,7,9,11,17,19,23,27)
type <- c('nom', 'ord', 'nom', 'scale', 'nom', 'scale', 'nom', 'scale', 'scale')

categories <- data.frame(category, subcategory, N, type)

However, I would like to obtain this dataframe:

category1 <- c('a','a','a','b','b','c','c')
subcategory1 <- c(1,2,3,4,5,6,7)
N1 <- c(34,7,9,28,19,23,27)
type1 <- c('nom, ord', 'nom', 'scale', 'nom, scale', 'nom', 'scale', 'scale')

categories1 <- data.frame(category1, subcategory1, N1, type1)

my try:

categories <- df %>%
  group_by(category, subcategory) %>%
  summarise(n = n(), unique_types = unique(type))

Unfortunately, this throws an error. Does anyone know how I can accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2025-02-02 06:58:32

您可以使用以下内容:

categories %>%
   group_by(category, subcategory) %>%
   summarise(N = sum(N), type = toString(unique(type)), .groups = 'drop')

 category subcategory     N type      
  <chr>          <dbl> <dbl> <chr>     
1 a                  1    34 nom, ord  
2 a                  2     7 nom       
3 a                  3     9 scale     
4 b                  4    28 nom, scale
5 b                  5    19 nom       
6 c                  6    23 scale     
7 c                  7    27 scale 

You can use the following:

categories %>%
   group_by(category, subcategory) %>%
   summarise(N = sum(N), type = toString(unique(type)), .groups = 'drop')

 category subcategory     N type      
  <chr>          <dbl> <dbl> <chr>     
1 a                  1    34 nom, ord  
2 a                  2     7 nom       
3 a                  3     9 scale     
4 b                  4    28 nom, scale
5 b                  5    19 nom       
6 c                  6    23 scale     
7 c                  7    27 scale 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文