Reputation: 51
I want to divide the percent into Korea and Taiwan separately.
I don't have any idea to calculate the percent with ddply
fucntion separately.
plot<-ddply(
data,
c("Country", "Here.is.usually.much.garbage.distributed."),
summarise,
n=length(Here.is.usually.much.garbage.distributed.),
percent=((n/sum(plot$n))*100)
)
Is there anyone who knows how to do it?
Upvotes: 0
Views: 163
Reputation: 887048
We can use data.table
library(data.table)
setDT(data)[, .(N = .N), by = .(Country, `Here.is.usually.much.garbage.distributed.`)][,
percent := N/sum(N) * 100][]
With ddply
, we get the count and do the percent outside
library(plyr)
out <-ddply(
data,
c("Country", "Here.is.usually.much.garbage.distributed."),
summarise,
n=length(`Here.is.usually.much.garbage.distributed.`)
)
out$perc <- out$n/sum(out$n) * 100
Upvotes: 0
Reputation: 388907
Consider switching to dplyr
instead of plyr
. Try using :
library(dplyr)
data %>%
group_by(Country, `Here.is.usually.much.garbage.distributed.`) %>%
summarise(n = n()) %>%
mutate(percent = n/sum(n) * 100)
Upvotes: 1