R generate all possible interaction variables

后端 未结 3 2118
既然无缘
既然无缘 2021-02-14 17:29

I have a dataframe with variables, say a,b,c,d

dat <- data.frame(a=runif(1e5), b=runif(1e5), c=runif(1e5), d=runif(1e5))

and would like to g

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-14 18:06

    Since model.matrix complains for factors with just one level, you alternatively might want to use stats::terms

    labels(terms(~.^2, data = iris[, 1:3]))
    # [1] "Sepal.Length"              "Sepal.Width"               "Petal.Length"             
    # [4] "Sepal.Length:Sepal.Width"  "Sepal.Length:Petal.Length" "Sepal.Width:Petal.Length"
    

提交回复
热议问题