Erasing columns containing NA's in their last 5 lines

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 the columns that in their last 5 values (line 46 to line 50) contain at least 1 NA value.

Well, I've been using dplyr a lot. Can I do this with him?

Any help?

    
asked by anonymous 27.09.2018 / 01:00

2 answers

7

Using dplyr :

df.final %>% 
  select_if(colSums(is.na(tail(., 5))) == 0)
    
27.09.2018 / 17:38
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)
    
27.09.2018 / 11:44