简体   繁体   中英

count number of rows where value in two columns are both true

I have a dataset with two columns of logicals, and I want to count the number of rows where the value of both columns is true.

Here's a sample dataset with two rows where both values are true:

df <- data.frame(
    column_A = c(TRUE, TRUE, FALSE, TRUE, FALSE),
    column_B = c(FALSE, TRUE, TRUE, TRUE, FALSE)
)

How would I count those two?

I know how to do this with dplyr (filter on the condition of both column A being TRUE and column B being true and then count) but I want to include this inside a function I'm writing and to be honest I don't understand how to do that since dplyr uses non-standard evaluation.

I feel like there's probably a very simple way to do this using a function in base R, but I'm lost and haven't had much luck googling. Any help would be much appreciated.

Simple! You can do sum(df$column_A & df$column_B) . df$column_A & df$column_B returns a logical vector, which can then be fed to sum() to determine how many values are TRUE .

In case you want to know the indices of the rows where both column_A and column_B are TRUE , you can use which(df$column_A & df$column_B) .

Reduce("&", df[c("column_A", "column_B")])
#[1] FALSE  TRUE FALSE  TRUE FALSE

You could use rowSums

sum(rowSums(df) == ncol(df))
#[1] 2

For doing this for selected columns, we can do

cols <- c("column_A", "column_B")
sum(rowSums(df[cols])  == length(cols))

We can also use apply

sum(apply(df[cols], 1, all))

Or with dplyr filter_at

library(dplyr)
df %>% filter_at(cols, all_vars(.)) %>% nrow

如果您只需要知道这两列,您可以获取条件的总和:

sum(df$column_A & df$column_B)

Or using tidyverse (similar to @db's base R method)

library(dplyr)
library(purrr)
df %>%
   summarise(out = sum(reduce(., `&`)))
#  out
#1   2

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