[英]Perform calculation on one column based on other columns in an R data frame
I have an R data frame that looks something like this: 我有一个R数据框,看起来像这样:
Company Date Number
ACoy 2015-08-28 1000
ACoy 2015-08-29 1300
ACoy 2015-08-30 1500
BCoy 2015-08-30 3000
CCoy 2015-08-30 2000
CCoy 2015-08-31 3000
ACoy 2015-08-31 1500
BCoy 2015-08-31 3000
CCoy 2015-09-01 3500
CCoy 2015-09-02 1000
ACoy 2015-09-02 900
CCoy 2015-09-03 2000
BCoy 2015-08-31 3000
CCoy 2015-08-31 3000
How can I perform a calculation on Number based on the value of Company, but only after a specific date? 我如何才能根据公司的价值(仅在特定日期之后)对数字进行计算?
Specifically, I am trying to get Number = Number/3
where Company == ACoy
and Date > 2015-08-30
具体来说,我正在尝试获取
Number = Number/3
,其中Company == ACoy
和Date > 2015-08-30
Result: 结果:
Company Date Number
ACoy 2015-08-28 1000
ACoy 2015-08-29 1300
ACoy 2015-08-30 1500
BCoy 2015-08-30 3000
CCoy 2015-08-30 2000
CCoy 2015-08-31 3000
ACoy 2015-08-31 500
BCoy 2015-08-31 3000
CCoy 2015-09-01 3500
CCoy 2015-09-02 1000
ACoy 2015-09-02 300
CCoy 2015-09-03 2000
BCoy 2015-08-31 3000
CCoy 2015-08-31 3000
This assumes that the Date
column is already classed as such. 假设
Date
列已经被分类。
## determine which rows match the specified condition
w <- with(df, Company == "ACoy" & Date > "2015-08-30")
## replace only those 'w' values with the specified calculation
df$Number <- replace(df$Number, w, df$Number[w] / 3)
## result
df
# Company Date Number
# 1 ACoy 2015-08-28 1000
# 2 ACoy 2015-08-29 1300
# 3 ACoy 2015-08-30 1500
# 4 BCoy 2015-08-30 3000
# 5 CCoy 2015-08-30 2000
# 6 CCoy 2015-08-31 3000
# 7 ACoy 2015-08-31 500
# 8 BCoy 2015-08-31 3000
# 9 CCoy 2015-09-01 3500
# 10 CCoy 2015-09-02 1000
# 11 ACoy 2015-09-02 300
# 12 CCoy 2015-09-03 2000
# 13 BCoy 2015-08-31 3000
# 14 CCoy 2015-08-31 3000
Here is an approach using data.table
. 这是使用
data.table
的方法。 We convert the 'data.frame' to 'data.table' ( setDT(df1)
). 我们将'data.frame'转换为'data.table'(
setDT(df1)
)。 Based on the condition in the 'i' ( Company=='ACoy' & Date > '2015-08-30'
), we assign 'Number' as the Number/3
. 根据'i'中的条件(
Company=='ACoy' & Date > '2015-08-30'
),我们将'Number'分配为Number/3
。
library(data.table)
setDT(df1)[Company=='ACoy' & Date > '2015-08-30', Number:= Number/3]
NOTE: We assume that 'Date' column is Date
class and the 'Number' is numeric
class. 注意:我们假设“日期”列为
Date
类,而“数字”为numeric
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.