Is any elegant solution to clean a dataframe from NA values without remove the row or column where the NA is?
Example:
Input dataframe
C1 C2 C3
R1 A <NA> <NA>
R2 <NA> A <NA>
R3 <NA> <NA> A
R4 B <NA> <NA>
R5 <NA> B <NA>
R6 <NA> <NA> <NA>
R7 C <NA> B
R8 C <NA>
R9 <NA>
R10 <NA>
R11 C
Output dataframe
C1 C2 C3
R1 A A A
R2 B B B
R3 C C C
For example, here is a messy dataframe (df1) full of NA values
A B C D E F G H I J K
1 Healthy <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
2 <NA> Healthy <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
3 <NA> <NA> Healthy <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
4 <NA> <NA> <NA> Healthy <NA> <NA> <NA> <NA> <NA> <NA> <NA>
5 <NA> <NA> <NA> <NA> Healthy <NA> <NA> <NA> <NA> <NA> <NA>
6 <NA> <NA> <NA> <NA> <NA> Healthy <NA> <NA> <NA> <NA> <NA>
Here is how it should be the dataframe.
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11
1 Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy
2 Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy
3 Healthy ICDAS_1_2 ICDAS_1_2 Healthy ICDAS_1_2 ICDAS_1_2 ICDAS_1_2 Healthy Healthy ICDAS_1_2 ICDAS_1_2
4 Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy
5 Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy
6 Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy Healthy
Note that the cell B-2 from the original dataframe now is in the X2-1. So the main issue here is to find the equivalent to "delete the cell and move all the cells up" function from Calc or Excel
All the answers that I found delete all the row or column where the <NA> value was. The way I managed to do it is (and sorry if this is primitive) was to extract only the valid values to a new dataframe:
First. I create an empty dataframe
library("data.table") # required package
new_dataframe <- data.frame(matrix("", ncol = 11, nrow = 1400) )
Then, I copy every valid value from the old to the new dataframe
new_dataframe$X1 <- df1$A[!is.na(df2$A)]
new_dataframe$X2 <- df1$B[!is.na(df2$B)]
new_dataframe$X3 <- df1$C[!is.na(df2$C)]
etc
So, my question is: is any more elegant solution to "clean" a dataframe from NA values?
Any help is greatly appreciated.