Search code examples
raggregate-functionsboolean-operations

Using boolean operator as a function in aggregate


I'm trying to perform an aggregate on two columns with a boolean operation as function. As an example, I'd like to perform the boolean AND on dataframe df between all the values of Column 2 where the values of Column 1 are the same. Here is my hypothetical aggregate operation:

 test <- aggregate(Col2~Col1, df, &)

Well, & is an operator, not a function. Is there a predefined AND function that I could use instead of the & operator or i actually have to define a function?

When i run the above code, i get the following error message:

Error: unexpected '&' in "test <- aggregate(Col2~Col1, df, &)

Basically, I'd like to know the neatest way to do this without using any extra packages.


Solution

  • It is better to use all or any in place of & or | respectively

    aggregate(Col2~Col1, df, all)
    

    data

    df <- data.frame(Col1 = rep(LETTERS[1:3], each = 3), 
       Col2 = c(TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE))