I am trying to get the list of variable names where the count of value "a" is more than or equals 2 and finally store it in a vector Morethan2 and similarly do for the lessthan2. Please help me to achieve this.
df <- data.frame(a1 = c("a","a","b"),a2 = c("a","b","b"),a3 = c("a","a","a")) for(x in names(df[1:3])){ if(sum(df[x] =="a") >= 2){ more2 = x } else{less2 = x}} Lessthan2 = less2 Morethan2 = more2 Expected REsult:
Morethan2 : 'a1','a3' Lessthan2 : 'a2'