Cartesian product data frame

后端 未结 7 2126
臣服心动
臣服心动 2020-11-29 18:47

I have three or more independent variables represented as R vectors, like so:

A <- c(1,2,3)
B <- factor(c(\'x\',\'y\'))
C <- c(0.1,0.5)
7条回答
  •  春和景丽
    2020-11-29 19:45

    Consider using the wonderful data.table library for expressiveness and speed. It handles many plyr use-cases (relational group by), along with transform, subset and relational join using a fairly simple uniform syntax.

    library(data.table)
    d <- CJ(x=A, y=B, z=C)  # Cross join
    d[, w:=f(x,y,z)]  # Mutates the data.table
    

    or in one line

    d <- CJ(x=A, y=B, z=C)[, w:=f(x,y,z)]
    

提交回复
热议问题