Search code examples
rsubsetcategorical-data

Is there a way to determine how many rows in a dataset have the same categorical variable for multiple conditions (columns)?


For example, i have the dataset below where 1 = yes and 0 = no, and I need to figure out how many calls were made by landline that lasted under 10 minutes.

Image of example dataset


Solution

  • We can use sum

    sum(df1[, "under 10 minutes"])
    

    If two columns are needed

    colSums(df1[, c("landline", "under 10 minutes")])
    

    If we are checking both columns, use rowSums

    sum(rowSums(df1[, c("landline", "under 10 minutes")], na.rm = TRUE) == 2)