如何对一个变量进行分组并根据另一个变量进行计数?

发布于 2025-01-13 17:30:09 字数 904 浏览 0 评论 0原文

是否可以使用group_by对一个变量进行分组并根据另一个变量对目标变量进行计数? 例如,

x1x2x3
A10
B21
C30
B11
A11

我想对 x3 的 0 和 1 进行计数,其中 x1

x1x3=0x3=1
A11
B02
C10

是是否可以使用 group_by 并添加一些内容来总结?我尝试了 group_by x1 和 x3,但这将 x3 作为第二列,这不是我们正在寻找的。

如果不可能只使用group_by,我想我们可以对x1和x3进行group_by,然后按x3分割并cbind它们,但是分割后的两个数据帧具有不同的行长度,并且没有cbind_fill。我应该做什么来绑定它们并填补额外的空白?

Is it possible to use group_by to group one variable and count the target variable based on another variable?
For example,

x1x2x3
A10
B21
C30
B11
A11

I want to count 0 and 1 of x3 with grouped x1

x1x3=0x3=1
A11
B02
C10

Is it possible to use group_by and add something to summarize? I tried group_by both x1 and x3, but that gives x3 as the second column which is not what we are looking for.

If it's not possible to just use group_by, I was thinking we could group_by both x1 and x3, then split by x3 and cbind them, but the two dataframes after split have different lengths of rows, and there's no cbind_fill. What should I do to cbind them and fill the extra blanks?

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

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

发布评论

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

评论(3

哑剧 2025-01-20 17:30:09

使用 data.table 包:

library(data.table)
dat <- as.data.table(dataset)
dat[, x3:= paste0("x3=", x3)]
result <- dcast(dat, x1~x3, value.var = "x3", fun.aggregate = length)

using the data.table package:

library(data.table)
dat <- as.data.table(dataset)
dat[, x3:= paste0("x3=", x3)]
result <- dcast(dat, x1~x3, value.var = "x3", fun.aggregate = length)
寻找我们的幸福 2025-01-20 17:30:09

使用 dplyr::count + tidyr::pivot_wider 实现所需结果的 tidyverse 方法:

library(dplyr)
library(tidyr)

df %>% 
  count(x1, x3) %>% 
  pivot_wider(names_from = "x3", values_from = "n", names_prefix = "x3=", values_fill = 0)
#> # A tibble: 3 × 3
#>   x1    `x3=0` `x3=1`
#>   <chr>  <int>  <int>
#> 1 A          1      1
#> 2 B          0      2
#> 3 C          1      0

DATA

df <- data.frame(
                x1 = c("A", "B", "C", "B", "A"),
                x2 = c(1L, 2L, 3L, 1L, 1L),
                x3 = c(0L, 1L, 0L, 1L, 1L)
)

A tidyverse approach to achieve your desired result using dplyr::count + tidyr::pivot_wider:

library(dplyr)
library(tidyr)

df %>% 
  count(x1, x3) %>% 
  pivot_wider(names_from = "x3", values_from = "n", names_prefix = "x3=", values_fill = 0)
#> # A tibble: 3 × 3
#>   x1    `x3=0` `x3=1`
#>   <chr>  <int>  <int>
#> 1 A          1      1
#> 2 B          0      2
#> 3 C          1      0

DATA

df <- data.frame(
                x1 = c("A", "B", "C", "B", "A"),
                x2 = c(1L, 2L, 3L, 1L, 1L),
                x3 = c(0L, 1L, 0L, 1L, 1L)
)
永言不败 2025-01-20 17:30:09

是的,这是可能的。这是一个例子:

dat = read.table(text = "x1     x2  x3
 A  1   0
 B  2   1
 C  3   0
 B  1   1
 A  1   1", header = TRUE)

dat %>% group_by(x1) %>% 
        count(x3) %>% 
        pivot_wider(names_from = x3,
                    names_glue = "x3 = {x3}", 
                    values_from = n) %>% 
        replace(is.na(.),0)

# A tibble: 3 x 3
# Groups:   x1 [3]
# x1    `x3 = 0` `x3 = 1`
#  <chr>     <int>     <int>
#1 A             1         1
#2 B             0         2
#3 C             1         0

Yes, it is possible. Here is an example:

dat = read.table(text = "x1     x2  x3
 A  1   0
 B  2   1
 C  3   0
 B  1   1
 A  1   1", header = TRUE)

dat %>% group_by(x1) %>% 
        count(x3) %>% 
        pivot_wider(names_from = x3,
                    names_glue = "x3 = {x3}", 
                    values_from = n) %>% 
        replace(is.na(.),0)

# A tibble: 3 x 3
# Groups:   x1 [3]
# x1    `x3 = 0` `x3 = 1`
#  <chr>     <int>     <int>
#1 A             1         1
#2 B             0         2
#3 C             1         0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文