首页 > 解决方案 > 如何在 R 编程中计算比例

问题描述

这是我的数据集

问题是如何计算数据集中超过 150 岁的男性比例?

标签: r

解决方案


您可以使用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)

部分

[1] 0.8


推荐阅读