根据条件将列除以值

时间:2018-08-17 08:05:06

标签: r

我想用数字除以一列,但是该数字会根据相应列的值而改变

主要数据框:

     ob_name reg_name Sales TVR
1  01-Jan-18   London 73000   0
2  08-Jan-18   London 54000   0
3  15-Jan-18   London 51000  22
4  22-Jan-18   London 50000  22
5  29-Jan-18   London 50000   0
6  05-Feb-18   London 71000   0
7  12-Feb-18   London 71000   0
8  01-Jan-18   Exeter   580   0
9  08-Jan-18   Exeter   760   0
10 15-Jan-18   Exeter   690   0
11 22-Jan-18   Exeter   620  32
12 29-Jan-18   Exeter   640  23
13 05-Feb-18   Exeter   520   0
14 12-Feb-18   Exeter   720   0

列的意思

  reg_name      Sales
1   Exeter   647.1429
2   London 60000.0000

输出(值除以reg_name的平均值)

 ob_name    reg_name    Sales   TVR
01-Jan-18   London  1.216666667 0
08-Jan-18   London        0.9   0
15-Jan-18   London       0.85   3.5
22-Jan-18   London  0.833333333 3.5
29-Jan-18   London  0.833333333 0
05-Feb-18   London  1.183333333 0
12-Feb-18   London  1.183333333 0
01-Jan-18   Exeter  0.896247241 0
08-Jan-18   Exeter  1.174392936 0
15-Jan-18   Exeter  1.066225166 0
22-Jan-18   Exeter  0.958057395 4.072727273
29-Jan-18   Exeter  0.988962472 2.927272727
05-Feb-18   Exeter  0.803532009 0
12-Feb-18   Exeter  1.112582781 0

到目前为止的代码...

regmean=aggregate(Sales~reg_name,data,mean)
#regmean=tapply(data$Sales,data$reg_name,mean)
i=intersect(regmean$reg_name,data$reg_name)
output<-data.frame(mapply(`/`, data[i,"reg_name"], regmean[i,"reg_name"]))

1 个答案:

答案 0 :(得分:0)

您可以使用group_by和mutate查找组的均值,然后定义比率

    data_LE <- read.csv("data_LE.csv", stringsAsFactors = F)

     data_LE%>%
      group_by(reg_name)%>%
      mutate(sal_mean=mean(Sales),TVR_mean=mean(TVR), sal_rt = Sales/sal_mean, TVR_rt= TVR/ TVR_mean)%>%
      select(-Sales, -TVR, - sal_mean, -TVR_mean)