Delete all rows with non-missing values in certain columns

I have some data.frames with different number of columns. For each such data.frame, I need to delete all rows with non-missing values in columns 2:ncol(data.frame)-1, which means all the columns except for the first and last columns. How could I do that?

How about drop_na() from tidyverse? You can put the columns to exclude in the brackets.

df <- df %>%
       drop_na(-first_column_name, -last_column_name)
1 Like

Could you please ask your questions with a minimal REPRoducible EXample (reprex)?

I know this question is not that complicated but It could bring speculation and unnecessary back and forths

Thanks. My task is computationally intensive, do we have a data.table version of drop_na?

If you need to subset a big dataframe it's often fast to do it like this:

keeps <- rowSums(is.na(df)) == 0 # identify rows to keep
df <- df[keeps, ] # keep them
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.