Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R创建乘法交互变量_R_Loops_Multiplication - Fatal编程技术网

使用R创建乘法交互变量

使用R创建乘法交互变量,r,loops,multiplication,R,Loops,Multiplication,我正试图编写一个函数来计算我指定的变量的所有乘法交互作用。参考下面的代码,了解我正在尝试做什么 mul <- function(data, vars) { for(ii in vars) { for(jj in vars[ii : length(vars)]) { data[, paste(ii, jj, sep = "mul")] <- data[, which(colnames(data) %in% ii)]*data[, which(colna

我正试图编写一个函数来计算我指定的变量的所有乘法交互作用。参考下面的代码,了解我正在尝试做什么

mul <- function(data, vars)
{
for(ii in vars)
 {
  for(jj in vars[ii : length(vars)])
  {
   data[, paste(ii, jj, sep = "mul")] <- 
   data[,  which(colnames(data) %in%   ii)]*data[,   which(colnames(data) %in% jj)]
  }

 }
test   
}

mul以下是我将如何做到这一点:

data('iris');
head(iris);
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
mul <- function(data, cols ) {
    for (i in 1:(length(cols)-1)) {
        for (j in (i+1):length(cols)) {
            col1 <- cols[i];
            col2 <- cols[j];
            data[,paste(col1,col2,sep='.mul.')] <- data[,col1]*data[,col2];
        };
    };
    data;
};
iris.mul <- mul(iris, names(iris)[1:4] );
head(iris.mul);
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length.mul.Sepal.Width Sepal.Length.mul.Petal.Length Sepal.Length.mul.Petal.Width Sepal.Width.mul.Petal.Length Sepal.Width.mul.Petal.Width Petal.Length.mul.Petal.Width
## 1          5.1         3.5          1.4         0.2  setosa                        17.85                          7.14                         1.02                         4.90                        0.70                         0.28
## 2          4.9         3.0          1.4         0.2  setosa                        14.70                          6.86                         0.98                         4.20                        0.60                         0.28
## 3          4.7         3.2          1.3         0.2  setosa                        15.04                          6.11                         0.94                         4.16                        0.64                         0.26
## 4          4.6         3.1          1.5         0.2  setosa                        14.26                          6.90                         0.92                         4.65                        0.62                         0.30
## 5          5.0         3.6          1.4         0.2  setosa                        18.00                          7.00                         1.00                         5.04                        0.72                         0.28
## 6          5.4         3.9          1.7         0.4  setosa                        21.06                          9.18                         2.16                         6.63                        1.56                         0.68
数据('iris');
头部(虹膜);
##萼片。长萼片。宽花瓣。长花瓣。宽种
##1 5.1 3.5 1.4 0.2刚毛
##2 4.9 3.0 1.4 0.2刚毛
##3 4.7 3.2 1.3 0.2刚毛
##4.6 3.1 1.5 0.2刚毛
##5.0 3.6 1.4 0.2刚毛
##6 5.4 3.9 1.7 0.4刚毛

mul完全避免循环,使用
combn

data(iris)
x <- names(iris)[1:4]
combn(x,2,FUN=function(x) iris[,x[1]] * iris[,x[2]]  )
#      [,1]  [,2]  [,3]  [,4] [,5]  [,6]
#[1,] 17.85  7.14  1.02  4.90 0.70  0.28
#[2,] 14.70  6.86  0.98  4.20 0.60  0.28
#[3,] 15.04  6.11  0.94  4.16 0.64  0.26
#[4,] 14.26  6.90  0.92  4.65 0.62  0.30
# etc etc
> combn(x, 2)
     [,1]           [,2]           [,3]           [,4]          
[1,] "Sepal.Length" "Sepal.Length" "Sepal.Length" "Sepal.Width" 
[2,] "Sepal.Width"  "Petal.Length" "Petal.Width"  "Petal.Length"
     [,5]          [,6]          
[1,] "Sepal.Width" "Petal.Length"
[2,] "Petal.Width" "Petal.Width" 
数据(iris)

x创建向量的所有双向组合的方法是使用
combn

data(iris)
x <- names(iris)[1:4]
combn(x,2,FUN=function(x) iris[,x[1]] * iris[,x[2]]  )
#      [,1]  [,2]  [,3]  [,4] [,5]  [,6]
#[1,] 17.85  7.14  1.02  4.90 0.70  0.28
#[2,] 14.70  6.86  0.98  4.20 0.60  0.28
#[3,] 15.04  6.11  0.94  4.16 0.64  0.26
#[4,] 14.26  6.90  0.92  4.65 0.62  0.30
# etc etc
> combn(x, 2)
     [,1]           [,2]           [,3]           [,4]          
[1,] "Sepal.Length" "Sepal.Length" "Sepal.Length" "Sepal.Width" 
[2,] "Sepal.Width"  "Petal.Length" "Petal.Width"  "Petal.Length"
     [,5]          [,6]          
[1,] "Sepal.Width" "Petal.Length"
[2,] "Petal.Width" "Petal.Width" 
然后可以迭代该字符矩阵的列:

comb.names <- combn(x, 2)
for ( i in 2:ncol(comb.names) ){ test <- cbind(test,
    iris[[ comb.names[1,i] ]]*iris[[ comb.names[2,i] ]])
    names(test)[i] <- paste( substr(comb.names[1,i],1,7), 
                             substr( comb.names[2,i],1,7),sep="_")}
 str(test)
#--------------------
'data.frame':   150 obs. of  6 variables:
 $ SepL.SepW      : num  17.8 14.7 15 14.3 18 ...
 $ Sepal.L_Petal.L: num  7.14 6.86 6.11 6.9 7 9.18 6.44 7.5 6.16 7.35 ...
 $ Sepal.L_Petal.W: num  1.02 0.98 0.94 0.92 1 2.16 1.38 1 0.88 0.49 ...
 $ Sepal.W_Petal.L: num  4.9 4.2 4.16 4.65 5.04 6.63 4.76 5.1 4.06 4.65 ...
 $ Sepal.W_Petal.W: num  0.7 0.6 0.64 0.62 0.72 1.56 1.02 0.68 0.58 0.31 ...
 $ Petal.L_Petal.W: num  0.28 0.28 0.26 0.3 0.28 0.68 0.42 0.3 0.28 0.15 ...
#------------------

combn
有一个
FUN=
参数,可将函数直接应用于组合,从而避免了循环yes的需要。如果SO接口允许的话,我会给你更多的quatloos。我认为值得指出的是,这并没有避免循环,它只是将它(在大量其他crud之间)隐藏在
combn()
中。而且,由于您调用了两次
combn()
,现在实际上算法中的迭代次数增加了一倍。Boav.@ BGOSTST——根据OP的问题,有100个变量,我会考虑计算速度的任何损失,以获得更大的可读性。实际上没有必要讽刺。这是公平的,尽管可以说显式循环比调用
combn()
更直接,因此可读性更强。但这是一个很好的解决方案,我为讽刺道歉+1感谢您纠正我的循环函数,这非常有帮助!
lm(as.numeric(Species) ~ (.)^2, data=iris)

Call:
lm(formula = as.numeric(Species) ~ (.)^2, data = iris)

Coefficients:
              (Intercept)               Sepal.Length  
                 4.425390                  -0.792828  
              Sepal.Width               Petal.Length  
                -1.119006                   0.228466  
              Petal.Width   Sepal.Length:Sepal.Width  
                 1.378179                   0.240113  
Sepal.Length:Petal.Length   Sepal.Length:Petal.Width  
                -0.004753                  -0.050226  
 Sepal.Width:Petal.Length    Sepal.Width:Petal.Width  
                -0.017482                  -0.356884  
 Petal.Length:Petal.Width  
                 0.135710