0

I am getting below error while trying to create a lagged column for my independent variable.

ML$AML1<-c(NA,ML$AML)

Error in `$<-.data.frame`(`*tmp*`, AML1, value = c(NA, 4.66, 4.81, 4.72,  :
  replacement has 11 rows, data has 10

Can anyone help me with coding here?

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    the dplyr package has a lag() function that does just this. – Phil Aug 10 '22 at 15:44
  • 1
    Does this answer your question? [Basic lag in R vector/dataframe](https://stackoverflow.com/questions/3558988/basic-lag-in-r-vector-dataframe) – jpsmith Aug 10 '22 at 16:01

1 Answers1

0

as @Phil pointed out in the comment there is a lag function in the dplyr package. First lets solve it the way you are trying to do it though. You are getting an the error because you just added one item to the start of the vector (df column). This makes it one item longer than the original dataframe itself (or other columns aka vectors), therefore you have to remove the last value as well to keep the original length:

ML$AML1 <- c(NA, ML$AML[1:(nrow(ML)-1)]) 
# you could use length(ML$AML) instead of nrow(ML)

Now the dplyr way:

library(dplyr)

dplyr::mutate(ML, AML1 = dplyr::lag(AML))
DPH
  • 4,244
  • 1
  • 8
  • 18