简体   繁体   中英

R: To identify whether column names in a dataframe contains string

Say I have 2 dataframes:

df1:

Name  Data123   Data321   Age
A     123       321       20

df2:

Name   Age
B      20

I wish to check which dataframe has column names containing the string "Data". If yes, the dataframe will be passed into a customized function as a whole. Thus in this case I wish to only have df1 passed into the said function. Please advise

We can use a function with grepl for reusability (from base R ), wrap with any to return a single TRUE/FALSE (if there is a column name with the substring 'Data' or not) and this can be used for the purpose mentioned

f1 <- function(dat, pat) any(grepl(pat, names(dat)))
f1(df1, '^Data')
f1(df2, '^Data')

Or with startsWith

f1 <- function(dat, pat) any(startsWith(names(dat), pat))
f1(df1, 'Data')

you can use str_detect() from stringr package:

library(stringr)
#does any (column) name inside df1 contains "Data"?
any(str_detect(names(df1), "Data"))

Data:

df1 <- data.frame(Name = "A", Data123 = 123, Data321 = 321, Age = 20)
df2 <- data.frame(Name = "B", Age = 20)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM