简体   繁体   English

在R中满足条件时向数据框添加值

[英]Add values to dataframe when condition met in R

I have a dataframe like this: 我有一个这样的数据框:

ID1   ID2   Position  Grade   Day
234   756      2        87     27
245   486      4        66     26
321   275      1        54     20
768   656      6        51     7
421   181      1        90     14
237   952      8        68     23
237   553      4        32     30

And I have another dataframe like this: 而且我有另一个像这样的数据框:

ID1   ID2   Day  Count
234   756    2     3 
245   486    2     1
209   706    2     1
124   554    2     2
237   553    2     4

I need to add the Counts to the first dataframe where the ID1, ID2 and Day are matched. 我需要将计数添加到ID1,ID2和Day匹配的第一个数据帧中。 However, I also need to have it so that if there is no match (no Counts in the second dataframe for the set of ID1, ID2 and Day in the first dataframe) then a zero is put in that place. 但是,我还需要它,以便如果不匹配(在第一个数据帧中ID1,ID2和Day的集合的第二个数据帧中没有计数),那么该位置将置零。 So the final dataframe would be something like: 因此,最终数据帧将类似于:

ID1   ID2   Position  Grade   Day   Count
234   756      2        87     27     3
245   486      4        66     26     1
321   275      1        54     20     0
768   656      6        51     7      0
421   181      1        90     14     0
237   952      8        68     23     0
237   553      4        32     30     4

This can be useful 这很有用

> # First, merge df1 and df2
> df3 <- merge(df1, df2, by=c("ID1", "ID2"), all.x=TRUE)
> # Replace NA with 0's
> transform(df3[, -6], Count=ifelse(is.na(Count), 0, Count))
  ID1 ID2 Position Grade Day.x Count
1 234 756        2    87    27     3
2 237 553        4    32    30     4
3 237 952        8    68    23     0
4 245 486        4    66    26     1
5 321 275        1    54    20     0
6 421 181        1    90    14     0
7 768 656        6    51     7     0

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

相关问题 R - 根据另一个数据框列中的值满足的条件在数据框列中添加值(由公式导出) - R - Add values (derived by a formula) in a dataframe column based on a condition met by values in a column of another dataframe R:在满足条件时将新行添加到 dataframe,然后重复最终值 - R: Add new rows to a dataframe when a condition is met and then repeat the final value 如果满足条件,则将整行添加到 R 中的新数据框中 - If a condition is met, add entire row to a new dataframe in R 仅当 r 中满足条件时,数据帧内的增量计数器 - incremental counter within dataframe only when a condition is met in r R:循环或过滤 dataframe 并在满足特定条件时停止 - R: loop or filter over the dataframe and stop when specific condition is met 在 R 中满足条件时如何转换矩阵中的值? - How to transform values in a Matrix when condition is met in R? 如果满足条件,则更改数据帧的值,否则保持不变 - change values of dataframe if a condition is met and leave unchanged if not 如何在 R dataframe 中满足条件后的第二天有条件地删除数据值? - How can I conditionally remove data values the day after a condition is met within an R dataframe? R-如果满足条件,则在Plotly中添加跟踪 - R - Add trace in Plotly if condition is met 在R中满足条件时如何分割行 - In R how to divide a row when a condition is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM