Could anybody help me writing an R script, melting 9 rows with several NA into 3 complete rows?
My question is similar to: combine rows in data frame containing NA to make complete row
Let's say, I have this data frame (with many more columns):
data <- data.frame('a' = c('1','2','3','4','5','6','7','8','9'),
'b' = c(1,2,NA,NA,NA,2,NA,NA,NA),
'c' = c(NA,1,3,NA,NA,NA,NA,NA,3),
'd' = c(NA,NA,NA,4,5,2,NA,NA,NA),
'e' = c(2,NA,NA,NA,NA,3,1,NA,NA),
'f' = c(NA,3,NA,NA,NA,NA,NA,5,3))
Edit thanks to your comments: There are always three observations per column except of column 'a' and later on I want to calculate ICCs. Column 'a' can be left out.
Desired Output would look like that:
b c d e f
1 1 1 4 2 3
2 2 3 5 3 5
3 2 3 2 1 3
I tried several codes recommended for similar problems and I already succesfully used coalesce() for melting two columns into one. However, I couldn't manage to make them work for my data yet. The group_by() function as suggested in the link above does not work in my script, because even if I recode the first vector as c(1,1,1,2,2,2,3,3,3) I did an 'artificial' selection and I still get rows with NAs (if I understood it right).
As I am still new at working with R, please explain it with two or three extra words :)