'Removing NAs from two columns in data frame a shift up

I have this data frame

                               atac.v1.pbmc.5k.possorted.bam.bam possorted.bam.bam
chr1.9941.10736                                 NA                NA
                                             13196              4348
chr1.10918.11476                                NA                NA
                                              2624               658
chr1.20874.21591                                NA                NA
                                               652               343

and I would like to remove the NAs and get this:

                               atac.v1.pbmc.5k.possorted.bam.bam possorted.bam.bam
chr1.9941.10736                              13196              4348                 
chr1.10918.11476                             2624               658                  
chr1.20874.21591                             652                343 
                                             

There are many questions about removing NAs, but I couldn't manage to use any of them to do this. I tried remove NAs and various code to remove white spaces, but wasn't able to.



Solution 1:[1]

For R a similar question was already asked here.

Thus when you want to solve it in R you can eg. use the following:

df = data.frame('name'=c('a',NA,'b',NA), 'val'=c(NaN, 1, NaN, 5))
library(dplyr)
na.omit(transform(df, name = lag(name)))

The df is only given to have a minimal running program.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SimonT