Search code examples
rsurvey

Count of specific value in column


I have data from survey questionnaire like this:

    survey <- data.frame(
    ID = sample(1:10),
    Var1 = sample(c("yes", "no"), 10, replace = TRUE),
    Var2=sample(c("DNK", "yes", "no"), 10, replace = TRUE),
    Var3=sample(c("DNK", "PNA", "yes", "no"), 10, replace = TRUE),
    Var4=sample(c("DNK", "PNA", "yes", "no"), 10, replace = TRUE)
   )

I want to know which variable has the most DNK and PNA responses.I think it would be a loop function but I can't seem to figure this out.
That is, I want an output with the count of PNA/DNK for each variable: e.g.)

Var3: DNK 4
Var3: DNK 3, PNA 1
Var4: DNK 1, PNA 3

Solution

  • Thank you for the replies. Using the responses provided, this is what I was looking for: dnk=apply(subset[,1:dim(survey)[2]], 2, function(x) length(which(x=="DNK")))