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.
>Solution :
One possibility:
has_0.5 <- apply(df, 1, function(x) any(grepl(0.5, x)))
df[has_0.5, ]