[R] Get a percent variable based on group
arun
smartpink111 at yahoo.com
Wed Jan 16 04:11:22 CET 2013
HI,
Not sure if this is what you meant.
tapply(iris$Sepal.Length,iris$Species,FUN=function(x) sum(x)/sum(iris$Sepal.Length)*100)
# setosa versicolor virginica
# 28.55676 33.86195 37.58129
A.K.
----- Original Message -----
From: Karine Charlebois <karine.charlebois at outlook.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Tuesday, January 15, 2013 9:30 PM
Subject: [R] Get a percent variable based on group
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)))
This does not work, I should have only three standard values, respectively for setosa, versicolor, and virginica. How can I do this?
MANY THANKS,
Karine
[[alternative HTML version deleted]]
______________________________________________
R-help at 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.
More information about the R-help
mailing list