简体   繁体   English

如何计算r中的比例?

[英]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!! 谢谢!!

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: 我不确定您想要什么,但很可能是其中一个在末尾的注释中可重复定义m情况之一:

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)

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

I guess what the OP wants is just this: 我猜想OP就是这样的:

Data (a data.frame x ): 数据(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: proportion部分只是一个新栏,其中“免疫接种”占“合格”的百分比:

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. 这是非常基本的,但这似乎是个问题。

Since you want ratio of sum of Immunised column with Eligible you could do 由于您希望“ Immunised列的总和与“ Eligible比率,您可以这样做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM