R-按值分组并计算整个组的百分比

时间:2018-10-08 15:28:11

标签: r

编辑:我的问题还不够清楚。我道歉。

问题在于定义组并为其分配数据框的列的值。 我自己用一系列ifelse和这里的评论解决了这个问题。感谢那。然后,我分别为每个列手动进行了操作。

data %>% 
  mutate(group = ifelse(richness <= -0.6, "1",
                        ifelse(richness > -0.6 & richness <= -0.2, "2",
                               ifelse(richness >-0.2 & richness <= 0.2, "3",
                                      ifelse(richness >0.2 & richness <= 0.6, "4",
                                             ifelse(richness >0.6, "5", NA)))))) %>%
                          group_by(group) %>% 
                          summarise(percentage=n()*100/"No.of.values")

2 个答案:

答案 0 :(得分:2)

carb数据集中的mtcars变量为例:

prop.table(table(mtcars$carb)) * 100

     1      2      3      4      6      8
21.875 31.250  9.375 31.250  3.125  3.125

如果您想定义自己的组,则可以使用cut函数:

groups <- c(0,2,6,8) # interval values for the groups
prop.table(table(cut(mtcars$carb, breaks=groups))) * 100

 (0,2]  (2,6]  (6,8]
53.125 43.750  3.125

答案 1 :(得分:0)

工作流程。

  • 添加虚拟列;
  • 按虚拟列分组;
  • 计算子组。

以下是一些示例代码:

require(dplyr)

# generate fake data.
set.seed(123456)

sample <-  data.frame(Nums = rep(NA,100))
sample$Nums <- sample(-100:100, 100, replace = T)/100 
size <- length(sample$Nums)

# add dummy column
sample <- sample %>% 
  # changed the dummy column accordingly
  mutate(dummy = ifelse(Nums < 0, "A", "B")) %>% 
  # group nums
  group_by(dummy) %>% 
  # calculate percentage
  summarise(percentage = n()*100/size)

head(sample)

# A tibble: 2 x 3
  dummy count percentage
  <chr> <int>      <dbl>
1 A        50         50
2 B        50         50