Question

I have some data I'm trying to analyze. I have two variables - one is a yes or no type question, the other has many possible answers (but categorical - like "not at all, a bit, neutral, lots, 100%").

I want to use tapply to calculate the proportion. I don't know how to do that. I know how to calculate mean, like this:

tapply(dataset$variable1, dataset$variable2, mean)

Anyways, I don't know how to get proportion instead of mean like this, but besides that, when I do try to get the mean, I get: 0 1 NA NA Because my data obviously has NA answers in it. I tried adding the "rm.na=TRUE" value, but that didn't fix it. So how do I get rid of these NA's? Or is there another way I could calculate proportions like that? (calculate: what proportion of people who anwered yes to question variable 1 also answered 3 to question variable 2?)

Was it helpful?

Solution

I think u need to use tapply(na.omit(dataset$variable1), na.omit(dataset$variable2), mean)

DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA))
DF
  x  y
1 1  0
2 2 10
3 3 NA
na.omit(DF)
  x  y
1 1  0
2 2 10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top