Suppose there is a dataframe with several measurements, where some measurements are missing. Where the value is missing, all following measurements are missing too. How can the last measurement be found?
df <- data.frame(id = c(1, 2, 3, 4), m_1 = c('a', 'b', 'c', 'd'), m_2 = c('e', NA, 'g', 'h'), m_3 = c('i', NA, NA, 'l'))
df
id m_1 m_2 m_3
[1] 1 a e i
[2] 2 b <NA> <NA>
[3] 3 c g <NA>
[4] 4 d h l
There are two options any of which I would like to get.
df
id m_1 m_2 m_3 m
[1] 1 a e i m_3
[2] 2 b <NA> <NA> m_1
[3] 3 c g <NA> m_2
[4] 4 d h l m_3
df
id m_1 m_2 m_3 m
[1] 1 a e i i
[2] 2 b <NA> <NA> b
[3] 3 c g <NA> g
[4] 4 d h l l
I was trying to mix mutate with which, colnames and is.na, but it did not work out.