I have a dataset of non-numeric sequences structured similarily to below (there are many more rows/col in the real dataset):
X1 X2 X3 X4 X5 X6 X7
1 A A C C B A A
2 A A NA NA NA B A
3 A C C NA NA B B
Each observation (ie. rows 1, 2, 3) are independent of each other. I would like to fill in the NA's with a combination of the values from the nearest non-NA's (from the same row). This would result in assigning, for example, a transition value 'A-B' for NA's between A and B (the values on either side of the NA). My ideal result would be:
X1 X2 X3 X4 X5 X6 X7
1 A A C C B A A
2 A A A-B A-B A-B B A
3 A C C C-B C-B B B
I am new to R and therefore unsure what approach to take. I have searched for solutions and have found some helpful ones eg. using na.locf to fill NA's with the last observation, but I can't figure out how to fill the data considering non-na values on the left and right sides in the same row. Any suggestions would be appreciated.
c(NA, NA,"B", "A", NA, "A", NA, NA)
? – RiskyMaor