J_S_L
J_S_L

Reputation: 43

How to calculate the proportion in r?

I have a data frame and I want to calculate the proportion. the table looks like this:

                Eligible        Immunised
 Auckland          1778            1426
 Bay of plenty     1194            802
 Canterbury        3461            2731

I want to know the proportion across all the districts of who were immunized. I think I need to plus the eligible column and immuised column together then use immunised divided by eligible. But I'm not too sure how to do the code. If anyone can help would be great. Thanks!!

Upvotes: 1

Views: 3738

Answers (4)

Henrique Florencio
Henrique Florencio

Reputation: 3751

Just divide both columns:

df$Proportion <- df$Immunised / df$Eligible

df
                Eligible        Immunised         Proportion
 Auckland          1778            1426            0.8020247
 Bay of plenty     1194            802             0.6716918
 Canterbury        3461            2731            0.7890783

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389325

Since you want ratio of sum of Immunised column with Eligible you could do

sum(df$Immunised)/sum(df$Eligible)
#[1] 0.770869

Upvotes: 0

vaettchen
vaettchen

Reputation: 7659

I guess what the OP wants is just this:

Data (a data.frame x):

dput( x )
structure(list(Region = c("Auckland", "Bay of plenty", "Canterbury"
), Eligible = c(1778L, 1194L, 3461L), Immunised = c(1426L, 802L, 
2731L)), .Names = c("Region", "Eligible", "Immunised"), 
class = "data.frame", row.names = c(NA, -3L))

The proportion part is just a new column with the Immunised as a percentage of the Eligible:

x$proportion = x$Immunised / x$Eligible
> x
         Region Eligible Immunised proportion
1      Auckland     1778      1426  0.8020247
2 Bay of plenty     1194       802  0.6716918
3    Canterbury     3461      2731  0.7890783

That's very basic but it seems to be the question.

Upvotes: 0

G. Grothendieck
G. Grothendieck

Reputation: 270348

I am not completely sure what you want but it is likely one of these where m is defined reproducibly in the Note at the end:

prop.table(m)
prop.table(m, 1)
prop.table(m, 2)
prop.table(colSums(m))
prop.table(rowSums(m))

Note

Next time please provide your input in a reproducible form. I have done it for you this time:

Lines <- "Eligible        Immunised
Auckland           1778            1426
Bay of plenty      1194            802
Canterbury         3461            2731"
L <- readLines(textConnection(Lines))
DF <- read.csv(text = gsub(" {5,}", ",", L), as.is = TRUE, strip.white = TRUE)
m <- as.matrix(DF)

Upvotes: 1

Related Questions