I want to extract a vector containing the names of all the variables whose values (not names themselves) contain a specific string.
For example:
> dat
Name Mark1 Mark2 Mark3
1 A 67% 61% 87
2 B 98% 83% 26
3 C 42% 62% 98
4 D 83% 32% 36
5 E 40% 90% 80
6 F 89% 25% 44
From the data frame above, I want the variable names whose values contain the '%' sign. As of now, I have been using a for-loop to do that, but it seems like a long way to do a simple task.
> prct <- c()
> for (i in 1:ncol(dat)){
if (any(grepl("%", dat[,i]) == T)){
prct <- c(prct, colnames(dat)[i])
}
}
> prct
[1] "Mark1" "Mark2"