Search code examples
rconditional-statementsfill

Conditionally fill R dataframe column, offset from a reference column


I have a dataframe called prices_df to which I add an extra column and conditionally fill, based on the contents of another column in the same df, with a 1

prices_df$itd_1 <- c(NA)
prices_df$itd_1 <- with( prices_df , ifelse(V2.1==1 , 1, NA) )

which gives a 1 in the last column thus

2005-11-16,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,1
2005-11-17,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA
2005-11-18,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA

However, what I would also like to do to is offset this conditional fill such that the output will be, for example, 1 in the column two rows later than the reference column, or perhaps x rows earlier or later, e.g.

2005-11-16,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA,NA
2005-11-17,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA
2005-11-18,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,1

How can I adjust the given code to achieve this offset conditional filling?


Solution

  • Best way is to:

    1. Create a time series object (I prefer zoo)
    2. Create your extra column with a lag (or offset as you call it)
    3. Merge everything together

      prices_df <- read.table(text="2005-11-16,NA,NA,NA,NA,1,NA,NA,NA,NA,NA,NA,NA
      2005-11-17,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA
      2005-11-18,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA", header=F, sep=",")
      
      library(zoo)
      prices_df.zoo <-zoo(prices_df[,-1],as.Date(prices_df[,1]))
      itd_1 <- lag(with( prices_df.zoo , ifelse(V6==1 , 1, NA) ),-2) #lag = -2
      prices_df.zoo <-merge(prices_df.zoo, itd_1)
      
      > prices_df.zoo
                 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 itd_1
      2005-11-16 NA NA NA NA  1 NA NA NA  NA  NA  NA  NA    NA
      2005-11-17 NA NA NA NA NA NA NA NA  NA  NA  NA  NA    NA
      2005-11-18 NA NA NA NA NA NA NA NA  NA  NA  NA  NA     1