Search code examples
rdataframer-colnames

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


Solution

  • 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')