条件百分比计算 - R.

时间:2018-05-30 20:30:42

标签: r dplyr conditional criteria percentage

我有df

> day <- c(1,1,1,1,2,2,2,2,3,3,3,3)
> Salesperson<- c("Chris", "Phil", "Joy", "Jess", "Chris", "Phil", "Joy", "Jess", "Chris", "Phil", "Joy", "Jess")
> Sales<-c(32,54,65,43,87,54,21,65,75,75,47,56)
> df <- cbind(day, Salesperson,Sales)
> df
      day Salesperson Sales
 [1,] "1" "Chris"     "32" 
 [2,] "1" "Phil"      "54" 
 [3,] "1" "Joy"       "65" 
 [4,] "1" "Jess"      "43" 
 [5,] "2" "Chris"     "87" 
 [6,] "2" "Phil"      "54" 
 [7,] "2" "Joy"       "21" 
 [8,] "2" "Jess"      "65" 
 [9,] "3" "Chris"     "75" 
[10,] "3" "Phil"      "75" 
[11,] "3" "Joy"       "47" 
[12,] "3" "Jess"      "56" 

我需要计算每个销售人员每天的销售百分比。我一直在尝试dpply或添加一个包含这些值的新列,但它们都不起作用。一个例子:

对于第1天,Chris和sales = 32,百分比应为32/(32+54+65+43)

1 个答案:

答案 0 :(得分:1)

无需多次使用group_by()和ungroup()。您可以使用tidyverse执行此操作:

day <- c(1,1,1,1,2,2,2,2,3,3,3,3)
Salesperson<- c("Chris", "Phil", "Joy", "Jess", "Chris", "Phil", "Joy", "Jess", "Chris", "Phil", "Joy", "Jess")
Sales<-c(32,54,65,43,87,54,21,65,75,75,47,56)
df <- cbind(day, Salesperson,Sales)

df <- as.data.frame(df)

df$Sales <- as.numeric(as.character(df$Sales))


df %<>% 
  group_by(day) %>% 
  mutate(perc = Sales / sum(Sales))

> df
A tibble: 12 x 4
Groups:   day [3]
   day   Salesperson Sales   perc
   <fct> <fct>       <dbl>  <dbl>
 1 1     Chris         32. 0.165 
 2 1     Phil          54. 0.278 
 3 1     Joy           65. 0.335 
 4 1     Jess          43. 0.222 
 5 2     Chris         87. 0.383 
 6 2     Phil          54. 0.238 
 7 2     Joy           21. 0.0925
 8 2     Jess          65. 0.286 
 9 3     Chris         75. 0.296 
10 3     Phil          75. 0.296 
11 3     Joy           47. 0.186 
12 3     Jess          56. 0.221 

PROOF:

df$perc[1]  == 32/(32+54+65+43)

给出

 [1] TRUE