I have data frame that has a column with large number of file names like:
d <- c(\"harry11_scott80_norm.avi\",\"harry11_norm.avi\",\"harry11_scott80_lpf.
Here's one way
> gsub(".*_scott80_.*", "incongruent", d)
[1] "incongruent" "harry11_norm.avi" "incongruent"
[4] "joel51_lpf.avi" "rich82_joel51_lpf.avi"
Or with grep
> d[grep("_scott80_", d)] <- "incongruent"
> d
[1] "incongruent" "harry11_norm.avi" "incongruent"
[4] "joel51_lpf.avi" "rich82_joel51_lpf.avi"
To address your edit, I believe this will do it (using |
to mean "or")
gsub(".*(_scott80_|_harry11_).*", "incongruent", d)
Of course, you don't have any strings in d
that match "_harry11_"
If your filenames are all of the same format, that is those with two names i.e harry11_scott80_norm.avi
always have two underscores, and those with one name i.e. harry11_norm.avi
always have one underscore, you can quickly use something like this to rename your files:
d = gsub(".*_.*_.*", "incongruent", d)
> d
[1] "incongruent" "harry11_norm.avi" "incongruent" "joel51_lpf.avi"
[5] "incongruent"
d =gsub(".*_.*","congruent",d)
> d
[1] "incongruent" "congruent" "incongruent" "congruent" "incongruent"