[英]Add column based on other columns values
I am honest, I could come up with a decent title for this.老实说,我可以为此想出一个像样的标题。 Basically, I have a
dateframe
:基本上,我有一个
dateframe
:
ID Qty BasePrice Total
1 2 30 50
1 1 20 20
2 4 5 15
For each line I want to calculate the following:对于每一行,我想计算以下内容:
Result = (Qty * BasePrice) - Total
Which is supposedly easy to do in R. However, I want to group the results by ID
(sum them).这在 R 中据说很容易做到。但是,我想按
ID
对结果进行分组(求和)。
Sample Output:示例输出:
ID Qty BasePrice Total Results
1 2 30 50 10
1 1 20 20 10
2 4 5 15 5
For instance, for ID=1
, the values represent ((2*30)-50)+((1*20)-20)
例如,对于
ID=1
,值表示((2*30)-50)+((1*20)-20)
Any idea on how can I achieve this?关于如何实现这一目标的任何想法?
Thanks!谢谢!
We can do a group_by
sum
of the difference between the product of 'Qty', 'BasePrice' with 'Total'我们可以对 'Qty'、'BasePrice' 与 'Total' 的乘积之差进行
group_by
sum
library(dplyr)
df1 %>%
group_by(ID) %>%
mutate(Result = sum((Qty * BasePrice) - Total))
# A tibble: 3 x 5
# Groups: ID [2]
# ID Qty BasePrice Total Result
# <int> <int> <int> <int> <int>
#1 1 2 30 50 10
#2 1 1 20 20 10
#3 2 4 5 15 5
df1 <- structure(list(ID = c(1L, 1L, 2L), Qty = c(2L, 1L, 4L), BasePrice = c(30L,
20L, 5L), Total = c(50L, 20L, 15L)), class = "data.frame", row.names = c(NA,
-3L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.