简体   繁体   中英

R language count rows of dataframe matching a criteria

I have a dataframe population with Height and Weight

    Height    Weight
1   1.4349813 53.73766
2   1.6875582 61.83858
3   1.4952729 51.64203
etc....

How can I count how many of these people have a height higher than 1.70 and a weight higher than 60?

I tried nrow(which(population$Height > 1.70 && population$Weight > 60))

您可以按照以下步骤进行:

sum(population$Height > 1.70 & population$Weight > 60)

With base R:

nrow(population[population$Height > 1.70 & population$Weight > 60, ])

With dpylr:

library(dpylr)

population %>% filter(Height > 1.70 & Weight > 60) %>% nrow()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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