How to subset entire data.frame

If my data.frame contains only numeric variables and I want to subset the entire data.frame on a condition (more than 0.5, for example) how do I do that in a tidy way?

Do you want to remove entire rows if any element in the row fails the filter condition or do you want to remove only the specific data points that fail the filter condition? In the latter case, you will probably end up with columns of different lengths, which is not allowed in a data frame.

1 Like

That is true, I think it's better to replace values with NAs, instead of filtering.

for mpg dataset , where there are numeric fields; if values are less than 2 make them NA instead.

library(dplyr)
mutate(mpg, across(where(is.numeric),
                  ~ifelse(.<2,NA,.)))

This topic was automatically closed 21 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.