一列中的每个不同项目中有多少分配给另一列中的每个不同项目?
How many of each distinct item in a column is assigned to each distinct item of another column?
我正在尝试使用 dplyr
函数 group_by()
和 summarize()
、count()
,但我不知道该怎么做:
每种颜色分配给每个 id 的数量是多少?即:id = 2 有 1 个 b 和 1 个 r。 id 3 只有 1 r ,...
df = data.frame(color = c("b","r","r","g","y","y","r"), id = c(2,3,2,6,4,4,7))
df %>%
group_by(id) %>%
summarize(count = n_distinct(color))
有没有明确的方法可以做到这一点?我会考虑创建假人,但我的真实数据集非常大,然后我想知道是否有一种简洁、漂亮的编码风格。感谢您的任何建议。
输出:
color id_2 id_3 id_4 id_6 id_7
1 b 1 0 0 0 0
2 g 0 0 0 1 0
3 r 1 1 0 0 1
4 y 0 0 2 0 0
您只是在寻找:
df %>%
group_by(id) %>%
count(color)
?
# A tibble: 6 x 3
# Groups: id [5]
id color n
<dbl> <chr> <int>
1 2 b 1
2 2 r 1
3 3 r 1
4 4 y 2
5 6 g 1
6 7 r 1
或者,如果您希望将 ID 作为列:
df %>%
group_by(id) %>%
count(color) %>%
pivot_wider(names_from = id,
names_prefix = 'id_',
values_from = n,
values_fill = 0) %>%
arrange(color)
# A tibble: 4 x 6
color id_2 id_3 id_4 id_6 id_7
<chr> <int> <int> <int> <int> <int>
1 b 1 0 0 0 0
2 g 0 0 0 1 0
3 r 1 1 0 0 1
4 y 0 0 2 0 0
感谢您添加预期的输出;这里有两个可能的解决方案:
library(tidyverse)
df = data.frame(color = c("b","r","r","g","y","y","r"),
id = c(2,3,2,6,4,4,7))
df %>%
group_by(id) %>%
count(color) %>%
pivot_wider(values_from = n,
names_from = id,
names_prefix = "id_",
values_fill = 0)
#> # A tibble: 4 × 6
#> color id_2 id_3 id_4 id_6 id_7
#> <chr> <int> <int> <int> <int> <int>
#> 1 b 1 0 0 0 0
#> 2 r 1 1 0 0 1
#> 3 y 0 0 2 0 0
#> 4 g 0 0 0 1 0
table(df$color, df$id)
#>
#> 2 3 4 6 7
#> b 1 0 0 0 0
#> g 0 0 0 1 0
#> r 1 1 0 0 1
#> y 0 0 2 0 0
另一种可能的方法是使用 tabyl()
from the janitor package,即
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
df %>%
tabyl(color, id)
#> color 2 3 4 6 7
#> b 1 0 0 0 0
#> g 0 0 0 1 0
#> r 1 1 0 0 1
#> y 0 0 2 0 0
这个包提供了比上面显示的其他方法更多的功能,例如将计数转换为百分比:
df %>%
tabyl(color, id) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 0)
#> color 2 3 4 6 7
#> b 100% 0% 0% 0% 0%
#> g 0% 0% 0% 100% 0%
#> r 33% 33% 0% 0% 33%
#> y 0% 0% 100% 0% 0%
或在百分比后加上括号中的总数:
df %>%
tabyl(color, id) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 0) %>%
adorn_ns()
#> color 2 3 4 6 7
#> b 100% (1) 0% (0) 0% (0) 0% (0) 0% (0)
#> g 0% (0) 0% (0) 0% (0) 100% (1) 0% (0)
#> r 33% (1) 33% (1) 0% (0) 0% (0) 33% (1)
#> y 0% (0) 0% (0) 100% (2) 0% (0) 0% (0)
由 reprex package (v2.0.1)
于 2022-03-25 创建
我正在尝试使用 dplyr
函数 group_by()
和 summarize()
、count()
,但我不知道该怎么做:
每种颜色分配给每个 id 的数量是多少?即:id = 2 有 1 个 b 和 1 个 r。 id 3 只有 1 r ,...
df = data.frame(color = c("b","r","r","g","y","y","r"), id = c(2,3,2,6,4,4,7))
df %>%
group_by(id) %>%
summarize(count = n_distinct(color))
有没有明确的方法可以做到这一点?我会考虑创建假人,但我的真实数据集非常大,然后我想知道是否有一种简洁、漂亮的编码风格。感谢您的任何建议。
输出:
color id_2 id_3 id_4 id_6 id_7
1 b 1 0 0 0 0
2 g 0 0 0 1 0
3 r 1 1 0 0 1
4 y 0 0 2 0 0
您只是在寻找:
df %>%
group_by(id) %>%
count(color)
?
# A tibble: 6 x 3
# Groups: id [5]
id color n
<dbl> <chr> <int>
1 2 b 1
2 2 r 1
3 3 r 1
4 4 y 2
5 6 g 1
6 7 r 1
或者,如果您希望将 ID 作为列:
df %>%
group_by(id) %>%
count(color) %>%
pivot_wider(names_from = id,
names_prefix = 'id_',
values_from = n,
values_fill = 0) %>%
arrange(color)
# A tibble: 4 x 6
color id_2 id_3 id_4 id_6 id_7
<chr> <int> <int> <int> <int> <int>
1 b 1 0 0 0 0
2 g 0 0 0 1 0
3 r 1 1 0 0 1
4 y 0 0 2 0 0
感谢您添加预期的输出;这里有两个可能的解决方案:
library(tidyverse)
df = data.frame(color = c("b","r","r","g","y","y","r"),
id = c(2,3,2,6,4,4,7))
df %>%
group_by(id) %>%
count(color) %>%
pivot_wider(values_from = n,
names_from = id,
names_prefix = "id_",
values_fill = 0)
#> # A tibble: 4 × 6
#> color id_2 id_3 id_4 id_6 id_7
#> <chr> <int> <int> <int> <int> <int>
#> 1 b 1 0 0 0 0
#> 2 r 1 1 0 0 1
#> 3 y 0 0 2 0 0
#> 4 g 0 0 0 1 0
table(df$color, df$id)
#>
#> 2 3 4 6 7
#> b 1 0 0 0 0
#> g 0 0 0 1 0
#> r 1 1 0 0 1
#> y 0 0 2 0 0
另一种可能的方法是使用 tabyl()
from the janitor package,即
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
df %>%
tabyl(color, id)
#> color 2 3 4 6 7
#> b 1 0 0 0 0
#> g 0 0 0 1 0
#> r 1 1 0 0 1
#> y 0 0 2 0 0
这个包提供了比上面显示的其他方法更多的功能,例如将计数转换为百分比:
df %>%
tabyl(color, id) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 0)
#> color 2 3 4 6 7
#> b 100% 0% 0% 0% 0%
#> g 0% 0% 0% 100% 0%
#> r 33% 33% 0% 0% 33%
#> y 0% 0% 100% 0% 0%
或在百分比后加上括号中的总数:
df %>%
tabyl(color, id) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 0) %>%
adorn_ns()
#> color 2 3 4 6 7
#> b 100% (1) 0% (0) 0% (0) 0% (0) 0% (0)
#> g 0% (0) 0% (0) 0% (0) 100% (1) 0% (0)
#> r 33% (1) 33% (1) 0% (0) 0% (0) 33% (1)
#> y 0% (0) 0% (0) 100% (2) 0% (0) 0% (0)
由 reprex package (v2.0.1)
于 2022-03-25 创建