Deleting columns containing NA s in their last 5 rows

Asked

Viewed 78 times

5

I have this personal dataframe,

set.seed(1)
df <- data.frame(A = 1:50, B = 11:60, c = 21:70)
head(df)
df.final <- as.data.frame(lapply(df, function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))

I want to delete columns that in their last 5 values (line 46 up to line 50) contain at least 1 NA value.

Well, I’ve been using the dplyr. I can do it with him?

Some help?

2 answers

7


Using the dplyr:

df.final %>% 
  select_if(colSums(is.na(tail(., 5))) == 0)

4

One of the solutions would be:

lims <- c(46, 50)

keep <- lapply(df[1:ncol(df)], function(x) sum(is.na(x[lims[1]:lims[2]])) == 0  )

keep <- unlist(keep)

df <- df[,keep]

tail(df)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.