生成R数据帧中的交互变量 [英] Generating interaction variables in R dataframes

查看:96
本文介绍了生成R数据帧中的交互变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了for循环以外,是否有一种方式在R数据帧中生成新的变量,这将是现有的两种可能的双向交互?
即假设具有三个数字变量V1,V2,V3的数据框,我想生成以下新变量:

  Inter.V1V2(= V1 * V2)
Inter.V1V3(= V1 * V3)
Inter.V2V3(= V2 * V3)
pre>

使用for循环的示例:

  x<  -  read .table(textConnection('
V1 V2 V3 V4
1 9 25 18
2 5 20 10
3 4 30 12
4 4 34 16'
),header = TRUE)

dim.init < - dim(x)[2]
for(i in 1:(dim.init - 1)){
(j in(i + 1):(dim.init)){
x [dim(x)[2] + 1] < - x [i] * x [j]
名称(x)[dim(x)[2]]< - paste(Inter.V,i,V,j,sep =)

}
}


解决方案

这是一个一个班轮,您有以下因素:

 > model.matrix(〜(V1 + V2 + V3 + V4)^ 2,x)
(截取)V1 V2 V3 V4 V1:V2 V1:V3 V1:V4 V2:V3 V2:V4 V3:V4
1 1 1 9 25 18 9 25 18 225 162 450
2 1 2 5 20 10 10 40 20 100 50 200
3 1 3 4 30 12 12 90 36 120 48 360
4 1 4 4 34 16 16 136 64 136 64 544
attr(,assign)
[1] 0 1 2 3 4 5 6 7 8 9 10


Is there a way - other than a for loop - to generate new variables in an R dataframe, which will be all the possible 2-way interactions between the existing ones? i.e. supposing a dataframe with three numeric variables V1, V2, V3, I would like to generate the following new variables:

Inter.V1V2 (= V1 * V2) 
Inter.V1V3 (= V1 * V3)
Inter.V2V3 (= V2 * V3)

Example using for loop :

x <- read.table(textConnection('
   V1 V2 V3 V4
1  9   25   18
2  5   20   10
3  4   30   12
4  4   34   16'
), header=TRUE)

dim.init <- dim(x)[2]
for (i in 1: (dim.init - 1) ) {
        for (j in (i + 1) : (dim.init) ) {
                x[dim(x)[2] + 1]    <- x[i] * x[j]
                names(x)[dim(x)[2]] <- paste("Inter.V",i,"V",j,sep="")

        }
}

解决方案

Here is a one liner for you that also works if you have factors:

> model.matrix(~(V1+V2+V3+V4)^2,x)
  (Intercept) V1 V2 V3 V4 V1:V2 V1:V3 V1:V4 V2:V3 V2:V4 V3:V4
1           1  1  9 25 18     9    25    18   225   162   450
2           1  2  5 20 10    10    40    20   100    50   200
3           1  3  4 30 12    12    90    36   120    48   360
4           1  4  4 34 16    16   136    64   136    64   544
attr(,"assign")
 [1]  0  1  2  3  4  5  6  7  8  9 10

这篇关于生成R数据帧中的交互变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆