Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Translating filter_all(any vars()) to base R

I have a dataframe with various numbers. What I want, is to subset rows using all column values.

One could use dplyr to write the following code:

library(dplyr)

set.seed(1)

df <- data.frame (matrix (round (runif(500, 0, 1), digits = 1), 10, 5))

dfn <- df |> dplyr::filter_all (dplyr::any_vars (grepl (0.5,.))) 

Does anyone know what the base R version of this code would be? Any help is very much appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One possibility:

has_0.5 <- apply(df, 1, function(x) any(grepl(0.5, x)))
df[has_0.5, ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading