Hello,
I took a code I viewed on this forum.
It just list the datasets that contains a variable equal to some string:
df1 <- tibble(score_a=1:20,
sex_a=rep(c("M", "F"), 10))
df2 <- tibble(score_b=1:20,
sex_b=rep(c("M", "F"), 10))
df3 <- tibble(score_c=1:20,
sex_c=rep(c("M", "F"), 10))
df4 <- tibble(score_c=1:20,
sex_c=rep(c("M", "F"), 10))
data_list<-list(df1,df2,df3,df4)
names(data_list)<-c("df1","df2","df3","df4")
names(data_list)
mifx <- function(data, text) {
data %>%
map_df(~names(.x)) %>%
pivot_longer(cols = everything(), names_to = "dataframe", values_to = "variables") %>%
filter(variables == text)
}
mifx(data_list,"score_a")
mifx list the dataframe and the variables with the match.
I rewrite the code in order to perfome a partial match, but It fails.
mifx2 <- function(data, text) {
data %>%
map_df(~names(.x)) %>%
pivot_longer(cols = everything(), names_to = "dataframe", values_to = "variables") %>%
filter(str_match(variables,text))
}
mifx2(data_list,"score")
What did I do wrong?
Thanks for your time and interest.
Have a nice day.