-2

I want to replace each missing value in the first column of my dataframe with the previous one multiplied by a scalar (eg. 3)

nRowsDf <- nrow(df)
for(i in 1:nRowsDf){
  df[i,1] =ifelse(is.na(df[i,1]), lag(df[i,1])+3*lag(df[i,1]),   df[i,1])
  }

The above code does not give me an error but does not do the job either.

In addition, is there a better way to do this instead of writing a loop?

Update and Data:

Here is an example of data. I want to replace each missing value in the first column of my dataframe with the previous one multiplied by a scalar (eg. 3). The NA values are in subsequent rows.

df <- mtcars
df[c(2,3,4,5),1] <-NA

IND <- is.na(df[,1])
df[IND,1] <- df[dplyr::lead(IND,1L, F),1] * 3

The last line of the above code does the job row by row (I should run it 4 times to fill the 4 missing rows). How can I do it once for all rows?

Hossein
  • 461
  • 3
  • 9
  • 17
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 26 '18 at 15:13
  • Possible duplicate of [Replace NA with previous or next value, by group, using dplyr](https://stackoverflow.com/questions/40040834/replace-na-with-previous-or-next-value-by-group-using-dplyr) – divibisan Sep 26 '18 at 18:57
  • ^^^ You should be able to figure out your problem using the answers on that question – divibisan Sep 26 '18 at 18:58

1 Answers1

1

reproducible data which YOU should provide:

df <- mtcars
df[c(1,5,8),1] <-NA

code:

IND <- is.na(df[,1])
df[IND,1] <- df[dplyr::lag(IND,1L, F),1] * 3

  • since you use lag I use lag. You are saying "previous". So maybe you want to use lead.
  • What happens if the first value in lead case or last value in lag case is missing. (this remains a mystery)
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
  • Thanks Andre! In my data I have NA values one after another. I am looking for a way to run your code for all rows once instead of running it for each row (As I explained in the update in the main post) – Hossein Sep 26 '18 at 21:23