On Jan 15, 2013, at 6:30 PM, Karine Charlebois wrote:

> Dear all, I'd like to get a percentage variable based on a group, but without 
> creating a new data frame. 
> For example:
> data(iris)
> 
> iris$percent <-unlist(tapply(iris$Sepal.Length,iris$Species,function(x) 
> x/sum(x, na.rm=TRUE)))

A percentage is 100 times a fraction whose nominal value is unity. My guess is 
that you want a percentage of the group mean? So this would just be:

iris$percent <-ave(iris$Sepal.Length, iris$Species, FUN=function(x) 
100*x/mean(x, na.rm=TRUE))

head(iris)

> 
> This does not work, I should have only three standard values, respectively 
> for setosa, versicolor, and virginica. How can I do this?

If you just want three values, then I do not see how these are percentages.

> tapply(iris$Sepal.Length,iris$Species,function(x) mean(x, na.rm=TRUE))
    setosa versicolor  virginica 
     5.006      5.936      6.588 


-- 
David Winsemius
Alameda, CA, USA

______________________________________________
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