Jin-Dominique
Jin-Dominique

Reputation: 3243

R: finding position of a file name that includes a certain keyword

I have the following entry at 1st row and 2nd column of the data frame df:

 df[1,2]<-"facebook-light.png,
           twitter-light.png, 
           linkedin-light.png, 
           logo.png, 
           screen4-smallBORDER.png,
           logo.png,
           screen4-smallBORDER.png,
           authorizereseller_0dd2310d24bc19a1bd3c1f950e45c34b.jpg,
           bcorporation_aa37be94146e3a0f8b85e7ecef0a49c4.jpg, 
           bayarea-greenbusiness_6c412f2f2f23c61f883e26ef015c5016.jpg,        
           betterbusinessbureau_7575c7b630f4d4593b1730df9d67cab3.jpg"

and I want to get the position of the file name that includes the termbcorp. The right answer in this particular case should be 9, since the bcorporation_aa37be94146e3a0f8b85e7ecef0a49c4.jpg, which includes the term bcorp, is the 9th file listed at df[1,2]. how can I compute such value?

thank you,

Upvotes: 0

Views: 55

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193667

It looks like you'll need a combination of strsplit to split up the string and grep to get the "position":

grep("bcorp", strsplit(df[1,2], ",", fixed = TRUE)[[1]])
[1] 9
## Or possibly
## grep("bcorp", strsplit(as.character(df[1,2]), ",", fixed = TRUE)[[1]])

Upvotes: 3

Related Questions