Search code examples
raggregate-functionsmultiplication

Multiplication in FUN argument


I have this dataframe

x <- data.frame(
        matrix(
          c(letters[1:3], c("x", "x", "y") ,
          sample(c(rep(1,100),0), size = 1),
          sample(c(rep(1,100),0), size = 1),
          sample(c(rep(1,100),0), size = 1)), ncol = 3)
)

I would like to do multiplication by group X and Y.

My suggestion

agg <- aggregate(x$X3,
                 by = list(x$X2),
                 FUN = *)

I would like to use something like sum, mean byt to multiply


Solution

  • + is to sum as * is to prod (for product).

    Your sample data follows the anti-pattern of data.frame(matrix()). A matrix can only have one data type. You mix character and numeric data in the matrix, and the matrix makes it all character class, and you can't do math on characters. Here's proper sample data and a demonstration the solution works. Also note that using by = X["X2"] instead of by = list(x$X2) gives a nicer column name in the result.

    (x <- data.frame(
              X1 = letters[1:3],
              X2 = c("x", "x", "y") ,
              X3 = 2:4
    ))
    #   X1 X2 X3
    # 1  a  x  2
    # 2  b  x  3
    # 3  c  y  4
    
    aggregate(x$X3, by = x["X2"], FUN = prod)
    #   X2 x
    # 1  x 6
    # 2  y 4