Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the column number from a dataframe which contains specific strings?

Tags:

dataframe

r

I have a data frame df with 7 columns and I have a list z containing multiple strings. I want a dataframe containing only the columns in df which contain the sting from z.

df <- data.frame("a_means","b_means","c_means","d_means","e_mean","f_means","g_means")
z <- c("a_m","c_m","f_m")

How do I get the column number of the z strings in df? Or how do I get a dataframe with only the columns which contains the z strings.

What I want is:

print(df)
"a_means" "c_m" "f_m"

What I tried:

match(a, names(df)

and

df[,which(colnames(df) %in% colnames(df[ ,grepl(z,names(df)])]
like image 284
Muesgen Avatar asked Dec 31 '22 22:12

Muesgen


2 Answers

You can use:

df[,match(z, substring(colnames(df), 1, 3))]
like image 171
Clemsang Avatar answered May 12 '23 18:05

Clemsang


With base R:

z <- paste(z, collapse = "|")
df[, grepl(z, names(df))]      # you could use grep as well
like image 21
h3rm4n Avatar answered May 12 '23 16:05

h3rm4n