I am trying to solve a little dilem with a data frame in R. I have the next data frame DF with this structure(I add dput() version in the final part):
Key M1 M2 M3 M4 M5 M6 M7
001 1 NA NA 1 NA NA 1
002 NA NA 1 NA NA 1 NA
003 NA NA NA 1 1 NA 1
004 NA NA 1 NA NA NA 1
005 NA NA NA 1 NA NA 1
006 1 NA NA NA NA NA NA
007 NA NA 1 NA NA NA 1
This data frame has and ID column (Key) and several columns with NA and 1. I want to fill each row that contains NA in the next pattern: When there are two NA and a previous 1 and a last 1 after the two NA, then both NA have to be filled with 1. If the pattern is not found, the elements in the rows must keep their original form. For example, in the first row there are two patterns: 1 NA NA 1 and 1 NA NA 1, then NA must be filled with 1. I would like to get a result like this:
Key M1 M2 M3 M4 M5 M6 M7
001 1 1 1 1 1 1 1
002 NA NA 1 1 1 1 NA
003 NA NA NA 1 1 NA 1
004 NA NA 1 NA NA NA 1
005 NA NA NA 1 1 1 1
006 1 NA NA NA NA NA NA
007 NA NA 1 NA NA NA 1
Where the patterns have been filled with 1. I have tried using na.locf() from zoo package, but it changes the rest of NAs in DF. The dput() version of DF is the next:
structure(list(Key = c("001", "002", "003", "004", "005", "006",
"007"), M1 = c(1, NA, NA, NA, NA, 1, NA), M2 = c(NA, NA, NA,
NA, NA, NA, NA), M3 = c(NA, 1, NA, 1, NA, NA, 1), M4 = c(1, NA,
1, NA, 1, NA, NA), M5 = c(NA, NA, 1, NA, NA, NA, NA), M6 = c(NA,
1, NA, NA, NA, NA, NA), M7 = c(1, NA, 1, 1, 1, NA, 1)), .Names = c("Key",
"M1", "M2", "M3", "M4", "M5", "M6", "M7"), row.names = c(NA,
-7L), class = "data.frame")
Many thanks for your help !