: Tuesday, January 15, 2013 10:22 PM
Subject: RE: [R] Get a percent variable based on group
For example,
iris$percent <- unlist(tapply(iris$Sepal.Length,iris$Species,function(x)
x/sum(iris$Sepal.Length, na.rm=TRUE)))
aggregate(iris$percent, by=list(iris$Species),
FUN=sum, na.rm=TRUE)
this l
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.
---
"
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,
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)))
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, respecti
5 matches
Mail list logo