简体   繁体   English

根据R数据帧中的其他列对一列执行计算

[英]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 == ACoyDate > 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.

相关问题 如何在数据框中的一列和所有其他列之间执行线性回归并将 r 平方值保存在新数据框中? - How to perform linear regression between one column and all other columns in a data frame and save r squared values in new data frame? 根据 R 中的一个列元素名称在数据框的其他列中创建一个元素 - create an element in other columns of a data frame based on one column element name in R 根据其他列数据框r中的值添加列 - add column based on values in other columns data frame r 根据 R 中数据框中的其他列创建新列 - Creating new column based on other columns in data frame in R 基于R中其他列中的数据的条件计算 - Conditional calculation based on data in other columns in R 在R中的其他数据帧的基础上,基于列添加column(Annotate)一个数据帧 - Add column(Annotate) one data frame based on column from other data frame in R R根据另外两列汇总一列中的数据 - R aggregate data in one column based on 2 other columns 使用apply suite根据一列中的值在数据帧的某些行上使用多个列执行函数 - Use apply suite to perform a function using multiple columns on certain rows of a data frame based on the values in one column 在数据框的一行上进行计算,另一行用于计算R中的收益 - Calculation on one row of a Data Frame with the other row for calculating returns in R R 计算来自其他列的新列值,使用相对位置。 为什么结果列的类= data.frame? - R calculation of new column value from other columns, using relative positions. Why does the class of the resulting column = data.frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM