Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groupby和count出现频率(以百分比表示,带R)_R - Fatal编程技术网

Groupby和count出现频率(以百分比表示,带R)

Groupby和count出现频率(以百分比表示,带R),r,R,给定如下数据集: city type 0 bj a 1 bj a 2 bj b 3 bj c 4 sh a 5 sh b 6 sh c 7 sh c 8 sh a 我需要按城市和类型分组,然后计算每个类型的发生频率百分比,这意味着每个城市的百分比总和应为1 df1 %>% filter(!is.na(city) & !is.na(type)) %>% group_by(city,

给定如下数据集:

  city type
0   bj    a
1   bj    a
2   bj    b
3   bj    c
4   sh    a
5   sh    b
6   sh    c
7   sh    c
8   sh    a
我需要按
城市
类型
分组,然后计算每个
类型
的发生频率百分比,这意味着每个
城市
的百分比总和应为
1

df1 %>%
filter(!is.na(city) & !is.na(type)) %>%
group_by(city, type) %>%
summarise(count = n() / nrow(.))
我尝试了以下代码,但似乎所有城市类型的百分比总和为
1

df1 %>%
filter(!is.na(city) & !is.na(type)) %>%
group_by(city, type) %>%
summarise(count = n() / nrow(.))
预期结果如下:

  city type  percent
0   bj    a     0.50  ---> 2/4
1   bj    b     0.25  ---> 1/4
2   bj    c     0.25  ---> 1/4
3   sh    a     0.40  ---> 2/5
4   sh    b     0.20  ---> 1/5
5   sh    c     0.40  ---> 2/5

基于上面的代码,我如何才能做到这一点?谢谢。

你可以
数一数
,然后计算每个
城市的比率

library(dplyr)

df %>%
  na.omit() %>% #Drop NA rows
  count(city, type) %>%
  group_by(city) %>%
  mutate(n = n/sum(n))

# city  type      n
# <chr> <chr> <dbl>
#1 bj    a      0.5 
#2 bj    b      0.25
#3 bj    c      0.25
#4 sh    a      0.4 
#5 sh    b      0.2 
#6 sh    c      0.4 
库(dplyr)
df%>%
na.omit()%>%#删除na行
计数(城市,类型)%>%
组别(城市)%>%
变异(n=n/和(n))
#城市类型n
#   
#1 bj a 0.5
#2 bj b 0.25
#3 bj c 0.25
#4 sh a 0.4
#5shb0.2
#6shc0.4

谢谢,我们如何在结果中添加
类型
?错过了。我们可以使用
变异
,更新答案。