简体   繁体   中英

How to select columns if there is not any NA in the last n observations? How to drop columns if there are more than x adjacent NA's observations?

I need the following:

1) Keep the columns if: i) The last n observations (n = 3) aren't NA's, ii) there is no NA's at all, iii) Backwards from the last NA's, there are not more than 3 adjacent NA observations

2) Drop the columns if: i) There are 3 or more adjacent NA observations

I'd like if the answer is using dplyr

Some example:

data = data.frame(
  A = c(3,3,3,3,4, rep(NA,5)),
  B = c(rnorm(10)),
  C = c(rep(NA,3), rnorm(7)),
  D = c(rnorm(8), NA, NA)
)

I've tried:

data %>% 
  select_if(~sum(!is.na(.)) >= 3)
  select_if(~sum(is.na(.)) > 0)

In my example, I'd only keep B, C and D.

We can use tail to get last n entries and drop the columns where all of them are NA .

n <- 3
library(dplyr)

data %>% select_if(~!all(is.na(tail(., n))))

#         B      C        D
#1   0.5697     NA  0.29145
#2  -0.1351     NA -0.44329
#3   2.4016     NA  0.00111
#4  -0.0392  0.153  0.07434
#5   0.6897  2.173 -0.58952
#6   0.0280  0.476 -0.56867
#7  -0.7433 -0.710 -0.13518
#8   0.1888  0.611  1.17809
#9  -1.8050 -0.934       NA
#10  1.4656 -1.254       NA

Or with inverted logic

data %>% select_if(~any(!is.na(tail(., n))))

For the second condition,

Drop the columns if: i) There are 3 or more adjacent NA observations

we can use rle to get adjacent values

data %>% select_if(~!any(with(rle(is.na(.)), lengths[values]) >= n))

#         B        D
#1   0.5697  0.29145
#2  -0.1351 -0.44329
#3   2.4016  0.00111
#4  -0.0392  0.07434
#5   0.6897 -0.58952
#6   0.0280 -0.56867
#7  -0.7433 -0.13518
#8   0.1888  1.17809
#9  -1.8050       NA
#10  1.4656       NA

Since we already have the functions, we can use the same in base R as well with sapply

#Condition 1
data[!sapply(data, function(x) all(is.na(tail(x, n))))]

#Condition 2
data[!sapply(data, function(x) any(with(rle(is.na(x)), lengths[values]) >= n))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM