Home > other >  Create new dataframe with first n non NA elements
Create new dataframe with first n non NA elements

Time:09-30

I want to create a new dataframe which removes all NAs from the top of each column. The resulting data frame will have n-maxNA rows, n is the size of the original data frame, maxNA is the max NA length for all columns. Here is a reproducible example.

This is the input

structure(list(X1 = c(NA, NA, NA, NA, 7L, 0L, 6L, 1L, 9L, 6L), 
    X2 = c(NA, NA, 1L, 10L, 2L, 9L, 1L, 8L, 4L, 1L), X3 = c(NA, 
    NA, NA, NA, NA, 6L, 8L, 6L, 9L, 8L), X4 = c(7L, 4L, 1L, 5L, 
    10L, 0L, 7L, 5L, 2L, 7L)), row.names = c(NA, -10L), class = "data.frame")

And the result should look like this

structure(list(x1 = c(7L, 0L, 6L, 1L, 9L), x2 = c(1L, 10L, 2L, 
9L, 1L), x3 = c(6L, 8L, 6L, 9L, 8L), x4 = c(7L, 4L, 1L, 5L, 10L
)), class = "data.frame", row.names = c(NA, -5L))

CodePudding user response:

We can loop over the columns of the data, remove the NA elements and get the first n elements with head. If there are less than 'n' non-NA elements in the column, it may return a list as data.frame requires all columns to be of equal length

sapply(df1, function(x) head(na.omit(x), 5))
      X1 X2 X3 X4
[1,]  7  1  6  7
[2,]  0 10  8  4
[3,]  6  2  6  1
[4,]  1  9  9  5
[5,]  9  1  8 10
  • Related