I have a dataframe with colnames similar to following:
dog_123456,
monkey_654534,
dog_123535,
cat_456452
bird_123458,
cat_123456,
crocodile_653769,
dog_132354,
cat_123333,
…
I wanna grab the columns that have an animal with specific codes. I made a vector “barcodes” which holds the numbers for colnames I want to collect and I tried to use grepl function:
colnames(data)[grepl(c(barcodes1), colnames(data))]
This will give me an error: “argument ‘pattern’ has length > 1 and only the first element will be used” and I only get the first hit “bird_123458” into my list.
Is there a way to make grepl (or some other function) to go through all the numbers stored in “barcodes” and print the full list?
Note: This is not my actual data but for simplicity I’m explaning it via similar case.