1

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

aynber
  • 22,380
  • 8
  • 50
  • 63
Agnes Lee
  • 322
  • 1
  • 12

2 Answers2

2

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')
akrun
  • 874,273
  • 37
  • 540
  • 662
0

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)
RaV
  • 617
  • 1
  • 5
  • 11