r – convenient way to select columns within a function based on regex within dplyr
I am trying to select columns based on a regex for a function within a dplyr command.
Maybe I am thinking to complicated or I have to reframe my data first?
I provide a reproducible example. I would love to define the columns to use inside length with a regular expression. So in my case col_of_interest
are all the columns with a
inside…
library(dplyr)
set.seed(1)
col_of_interest <- 'a'
df <-
data.frame(
id=rep(1:2, each=5),
a1=rnorm(10),
a2=c(NA, NA, NA, rnorm(7)),
b1=rnorm(10)
)
df %>%
group_by(id) %>%
reframe(value_count=length(na.omit(c(a1, a2))))
Read more here: Source link