[R] Get a percent variable based on group

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Wed Jan 16 07:12:48 CET 2013


As others have said, your goal is unclear to us. However, one guess I have not seen others make is if you are looking for a way to normalize within each group, perhaps you should look at

?ave

which typically creates a vector just as long as your data vector and grouping vector.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Karine Charlebois <karine.charlebois at outlook.com> 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)))
>
>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