[英]How to calculate proportion in R Programming
Question is how do I calculate the proportion of males over the height of 150 are in the dataset? 问题是如何计算数据集中150个身高以上的男性比例?
You can use subset
to filter data based on a condition and then compute ratio of lengths of the two dataframes (original and filtered). 您可以使用
subset
根据条件过滤数据,然后计算两个数据帧(原始数据和已过滤数据)的长度之比。
weight <- c(76,69,64,62,62,57,55,62,60,55)
height <- c(167,167,168,168,168,168,168,168,149,140)
data.foo <- cbind(weight, height) # Example dataset with only height and weight columns
over.150 <- subset(data.foo, height > 150) # filtered data
proportion <- nrow(over.150)/nrow(data.foo)
proportion
比例
[1] 0.8
[1] 0.8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.