简体   繁体   English

匹配日期和类别然后替换 r 中的值

[英]Match date and category then replace value in r

I have Scenario A and B which I have recorded values on a certain date range.我有场景 A 和 B,我记录了某个日期范围内的值。 I would like to replace all the zero (0) values in Scenario B with value in Scenario A where the dates are the same.我想用场景 A 中日期相同的值替换场景 B 中的所有零 (0) 值。

Example例子

On.Date     Scenario   Value
2019-01-01      A       1
2019-01-02      A       2
2019-01-03      A       3
2019-01-04      A       4
2019-01-05      A       5
2019-01-01      B       1
2019-01-02      B       0
2019-01-03      B       1
2019-01-04      B       0
2019-01-05      B       1

Solution I am looking for我正在寻找的解决方案

On.Date     Scenario   Value
2019-01-01      A       1
2019-01-02      A       2
2019-01-03      A       3
2019-01-04      A       4
2019-01-05      A       5
2019-01-01      B       1
2019-01-02      B       2
2019-01-03      B       1
2019-01-04      B       4
2019-01-05      B       1

I have tried if_else statements, mutate_if, matches, gathers and a whole host of others that I have found but I can't seem to get it to work.我已经尝试过 if_else 语句、mutate_if、matches、gathers 以及我发现的许多其他语句,但我似乎无法让它工作。

Any help would be greatly appreciated!任何帮助将不胜感激!

dt <- rbind(data.frame(On.Date = seq.Date(as.Date('2019-01-01'), 
                                          as.Date('2019-01-05'), by = 1), 
                       Scenario = "A",
                       value = rep_len(1:5, 5)), 
            data.frame(On.Date = seq.Date(as.Date('2019-01-01'), 
                                               as.Date('2019-01-05'), by = 1), 
                            Scenario = "B",
                            value = rep_len(1:0, 5)))

We can first find out indices where Scenario == "B" and value = 0 .我们可以首先找出Scenario == "B"value = 0索引。 Then we match those On.Date with the dates where Scenario == "A" and get respective value .然后我们match那些On.DateScenario == "A"的日期match并获得相应的value

inds <- which(dt$Scenario == "B" & dt$value == 0)
dt$value[inds] <- dt$value[match(dt$On.Date[inds], dt$On.Date[dt$Scenario == "A"])]

dt
#      On.Date Scenario value
#1  2019-01-01        A     1
#2  2019-01-02        A     2
#3  2019-01-03        A     3
#4  2019-01-04        A     4
#5  2019-01-05        A     5
#6  2019-01-01        B     1
#7  2019-01-02        B     2
#8  2019-01-03        B     1
#9  2019-01-04        B     4
#10 2019-01-05        B     1

Considering your data has a structure as in the example, one dplyr possibility could be:考虑到您的数据具有示例中的结构,一种dplyr可能性可能是:

dt %>%
 group_by(On.Date) %>%
 mutate(value = if_else(value == 0, first(value), value))

   On.Date    Scenario value
   <date>     <fct>    <int>
 1 2019-01-01 A            1
 2 2019-01-02 A            2
 3 2019-01-03 A            3
 4 2019-01-04 A            4
 5 2019-01-05 A            5
 6 2019-01-01 B            1
 7 2019-01-02 B            2
 8 2019-01-03 B            1
 9 2019-01-04 B            4
10 2019-01-05 B            1

An option in base R with ave带有ave base R的选项

dt$value <-  with(dt, ave(value, On.Date, FUN = function(x) replace(x, !x, x[1])))
dt$value
#[1] 1 2 3 4 5 1 2 1 4 1

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

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