Dear R listers,

I am trying to compute the mean of a dummy variable that is encoded as a
factor. However, even though the levels of my factor are 0 - 1, when I
compute the mean (after coercing the factor to be
numeric), R changes 0 into 1 and 1 into yes, thus altering my expected
result.

Please, consider the following working example:
pp <- rep(0:1, 10)
pp <- factor(pp, levels=(0:1), labels=c("no","yes"))
mean(pp) #this won't work because the argument is not numeric or logical
mean(as.integer(pp)) # this computes the average, but not on the range 0-1,
but 1-2. Indeed, the result is 1.5 and not 0.5 as expected.

What am I doing wrong?
Thanks in advance for your kind support,
f.


-- 
Francesco Sarracino, Ph.D.
https://sites.google.com/site/fsarracino/

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to