简体   繁体   English

R:如何根据列条件和另一个df的百分比值执行(用户定义的)计算?

[英]R: How to perform a (user defined) calculation based on column criteria and percentage values of another df?

I want to calculate the percentage values for a data.frame column based on criterion of another object's column using percentages of another df. 我想基于另一个对象的列使用另一个df的百分比的标准来计算data.frame列的百分比值。 Here is some example data: 以下是一些示例数据:

   df <- data.frame(Value = c(50,10,30,40),
                    object = c("apples","tomatoes", "apples","pears" ))

and a share object with the percentages for the objects: 以及一个共享对象及其百分比:

share <- data.frame(object = c("tomatoes","pears", "apples" ),
                   percentage = c(90,75,80))

My expected result is: 我的预期结果是:

  Value   object
1    40   apples
2     9 tomatoes
3    24   apples
4    30    pears

With only one object I used this code, which works fine: 我只有一个对象使用了此代码,效果很好:

df[,1] <- df$Value * share$percentage /100 

But how to perform this for the different objects in df/share? 但是如何对df / share中的不同对象执行此操作? "aggregation"/"subset" or something like that? “聚合” /“子集”之类的?

EDIT: I have tried this so far, which I think it could be a way: 编辑:到目前为止,我已经尝试过了,我认为这可能是一种方法:

df[,1] <- aggregate(df$Value, by=df$object,FUN= function(x) {x$Value * share$percentage /100}   )

and

df[,1] <- apply(split(df, df$object) function(x) {x$Value * share$percentage / 100})

However, the function seems to be wrong (I'm nor really familar with functions:). 但是,该函数似乎是错误的(我也不是很熟悉函数:)。 Please note, that I don't want first to merge df with share as answered, as I need the original structure of df and order. 请注意,由于我需要df和order的原始结构,因此我不想首先将dfshare合并为已回答。 So I'm looking for a short command. 因此,我正在寻找一个简短的命令。 I really appreciate your ideas. 我真的很感谢你的想法。 Thanks 谢谢

Do you want a weighted average? 您是否需要加权平均值?

library(dplyr)

df %>%
  inner_join(share) %>%
  group_by(object) %>%
  summarize(weighted_average = 
              sum(Value * percentage) / sum(percentage) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM