简体   繁体   English

根据另一个不同维度的 data.table 中的多个条件,选择性地更改一个 data.table 中的列

[英]Selectively alter a column in one data.table based on multiple conditions in another data.table of different dimensions

Example:例子:

Trade <- data.table (
  Ticker = c("ABC", "DEF", "GHI", "GHI", "JKL", "JKL", "MNO"),
  TPrice = c(100, 200, 300, 305, 400, 405, 500),
  Code = c("O", "O", "O", "C", "O", "C", "C")
)

MTM <- data.table (
  Ticker = c("ABC", "DEF", "GHI", "JKL", "MNO"), 
  Flag = c(1, 0, 0, 1, 1), 
  TPrice = 0L
)

I want the TPrice in the MTM data.table to become non-zero only when the same Ticker shown in both the Trade and the MTM data.tables have both a Flag of 1 (in the MTM data.table) and the Code is an "O" (in the Trade data.table).我希望 MTM data.table 中的 TPrice 仅在 Trade 和 MTM data.tables 中显示的相同代码都具有 1 标志(在 MTM data.table 中)且代码为“O”时才变为非零"(在贸易数据表中)。 The resulting entry in the MTM TPrice column is the TPrice from the Trade data.table when those conditions are met.当满足这些条件时,MTM TPrice 列中的结果条目是交易 data.table 的 TPrice。

Results I'm looking for:我正在寻找的结果:

MTM <- data.table (
  Ticker = c("ABC", "DEF", "GHI", "JKL", "MNO"), 
  Flag = c(1, 0, 0, 1, 1), 
  TPrice = c(100, 0, 0, 400, 0)
)

    Ticker Flag TPrice
1:    ABC    1    100
2:    DEF    0      0
3:    GHI    0      0
4:    JKL    1    400
5:    MNO    1      0

SO Posts Consulted咨询过的SO帖子

Either the question was not the same, often because my data.tables are of uneven dimensions, or I could not adapt the answer to my problem (still learning R) or I simply did not understand the answer to attempt an adaptation (I asked questions in the comment sections):要么问题不一样,通常是因为我的 data.tables 尺寸不均匀,要么我无法适应我的问题的答案(仍在学习 R)或者我根本不理解尝试适应的答案(我问了问题在评论区):

Subset a data frame based on another 基于另一个数据框子集

How extract values of a data.table based on multiple conditions? 如何根据多个条件提取 data.table 的值?

Update data.table based on multiple columns and conditions 根据多列和条件更新 data.table

Efficient way to subset data.table based on value in any of selected columns 根据任何选定列中的值对 data.table 进行子集化的有效方法

How can one work fully generically in data.table in R with column names in variables 如何在 R 中使用变量中的列名完全通用地工作 data.table

Multiple variable filters in r r中的多变量过滤器

Filter data.table on same condition for multiple columns 在相同条件下为多列过滤 data.table

R Data.table divide values in column based on another column R Data.table 根据另一列划分列中的值

The last SO post seemed the closest to my problem due to the uneven dimensions posited in the question, but I could not adapt the solution to my problem.由于问题中存在不均匀的维度,最后一篇 SO 帖子似乎最接近我的问题,但我无法使解决方案适应我的问题。

I would very much appreciate some help here.我非常感谢这里的一些帮助。

You can join the two dataframes and then check for the condition for each value of Ticker .您可以加入两个数据框,然后检查Ticker的每个值的条件。

library(data.table)

Trade[MTM, on = 'Ticker'][, .(Tprice = if(any(Code == 'O' & Flag == 1)) 
           TPrice[Code == 'O' & Flag == 1] else 0), .(Ticker, Flag)]

#   Ticker Flag Tprice
#1:    ABC    1    100
#2:    DEF    0      0
#3:    GHI    0      0
#4:    JKL    1    400
#5:    MNO    1      0

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

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