Home > database >  Conditionally update the value in column using lag value
Conditionally update the value in column using lag value

Time:07-18

I am trying to update the value in a RAIN column in following dataframe using ifelse condition. If value of RAIN is less than value of RAIN from previous row then replace that value with value from previous row. The sample dataframe is as below:

TRGCODE    gr      DATE      TIME     RAIN
   12      24   2017-03-15   1645     00
   12      24   2017-03-15   1700     00
   12      24   2017-03-15   1715     0.5
   12      24   2017-03-15   1730     00
   12      24   2017-03-15   1745     0.5
   12      24   2017-03-15   1800     0.5
   12      24   2017-03-15   1815     00
   12      24   2017-03-15   1830     00
   12      24   2017-03-15   1845     0.5
   12      24   2017-03-15   1900     00
   12      24   2017-03-15   1915     00
   12      24   2017-03-15   1930     0.5
   12      24   2017-03-15   1945     0.5

I tried with

df <- df %>% group_by(TRGCODE, gr) %>% dplyr::mutate(RAIN = ifelse(RAIN < lag(RAIN, default = first(RAIN)), lag(RAIN), RAIN))

But it's not the working the way I want (It will change value only for time 1730, 1815, 1900 and not for 1830 & 1915). The desired output is like:

TRGCODE    gr      DATE      TIME     RAIN
   12      24   2017-03-15   1645     00
   12      24   2017-03-15   1700     00
   12      24   2017-03-15   1715     0.5
   12      24   2017-03-15   1730     0.5
   12      24   2017-03-15   1745     0.5
   12      24   2017-03-15   1800     0.5
   12      24   2017-03-15   1815     0.5
   12      24   2017-03-15   1830     0.5
   12      24   2017-03-15   1845     0.5
   12      24   2017-03-15   1900     0.5
   12      24   2017-03-15   1915     0.5
   12      24   2017-03-15   1930     0.5
   12      24   2017-03-15   1945     0.5

CodePudding user response:

Here is one potential solution:

library(tidyverse)

df <- read.table(text = "TRGCODE    gr      DATE      TIME     RAIN
   12      24   2017-03-15   1645     00
   12      24   2017-03-15   1700     00
   12      24   2017-03-15   1715     0.5
   12      24   2017-03-15   1730     00
   12      24   2017-03-15   1745     0.5
   12      24   2017-03-15   1800     0.5
   12      24   2017-03-15   1815     00
   12      24   2017-03-15   1830     00
   12      24   2017-03-15   1845     0.5
   12      24   2017-03-15   1900     00
   12      24   2017-03-15   1915     00
   12      24   2017-03-15   1930     0.5
   12      24   2017-03-15   1945     0.5",
   header = TRUE)

df %>%
  group_by(TRGCODE, gr) %>%
  mutate(RAIN = cummax(RAIN))
#> # A tibble: 13 × 5
#> # Groups:   TRGCODE, gr [1]
#>    TRGCODE    gr DATE        TIME  RAIN
#>      <int> <int> <chr>      <int> <dbl>
#>  1      12    24 2017-03-15  1645   0  
#>  2      12    24 2017-03-15  1700   0  
#>  3      12    24 2017-03-15  1715   0.5
#>  4      12    24 2017-03-15  1730   0.5
#>  5      12    24 2017-03-15  1745   0.5
#>  6      12    24 2017-03-15  1800   0.5
#>  7      12    24 2017-03-15  1815   0.5
#>  8      12    24 2017-03-15  1830   0.5
#>  9      12    24 2017-03-15  1845   0.5
#> 10      12    24 2017-03-15  1900   0.5
#> 11      12    24 2017-03-15  1915   0.5
#> 12      12    24 2017-03-15  1930   0.5
#> 13      12    24 2017-03-15  1945   0.5

Created on 2022-07-18 by the reprex package (v2.0.1)

CodePudding user response:

This does not use the ifelse but it does the job you want

df<-structure(list(TRGCODE=c(12L,12L,12L,12L,12L,12L,12L,
                             12L,12L,12L,12L,12L,12L),gr=c(24L,24L,24L,24L,24L,
                                                           24L,24L,24L,24L,24L,24L,24L,24L),DATE=c("2017-03-15",
                                                                                                   "2017-03-15","2017-03-15","2017-03-15","2017-03-15","2017-03-15",
                                                                                                   "2017-03-15","2017-03-15","2017-03-15","2017-03-15","2017-03-15",
                                                                                                   "2017-03-15","2017-03-15"),TIME=c(1645L,1700L,1715L,1730L,
                                                                                                                                     1745L,1800L,1815L,1830L,1845L,1900L,1915L,1930L,1945L
                                                                                                   ),RAIN=c(0,0,0.5,0,0.5,0.5,0,0,0.5,0,0,0.5,0.5
                                                                                                   ),RAIN1=c(0,0,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
                                                                                                             0.5,0.5)),row.names=c(NA,-13L),)

df$RAIN1<-df$RAIN
for(i in 2:nrow(df)) {
  if(df$RAIN1[i]<df$RAIN1[i-1])
    df$RAIN1[i]<-(df$RAIN1[i-1])
}
df

df$RAIN<-df$RAIN1
df$RAIN1<-NULL
df
  • Related