【问题标题】:Substitution of a complex for loop in R在R中替换复杂的for循环
【发布时间】:2020-10-04 03:49:04
【问题描述】:

我想知道如何将嵌套的 for 循环替换为 R 编程中更简洁的形式。

我的变量名是:

names(Auto[-1])

“气缸”“排量”“马力”“重量”“加速度”“年份”“产地”

所以我做了一个简单的 for 循环:

for (var1 in names(Auto[-1]))
    {
      listanova[var] =  summary(lm((paste('mpg ~',var1)), data = Auto))[6]$sigma
      }

我怎样才能做一个更复杂的 for 循环而不是嵌套,我想避免长而复杂的 for 循环。例如,我想迭代 var1*var2+var3 的所有可能性,其中这些变量是 Auto[-1] 的变量。有可能吗?

【问题讨论】:

    标签: r for-loop


    【解决方案1】:

    我认为mpg ~ Var1 * Var2 + Var3 需要combn 2 个变量与expand.grid 的组合,而不仅仅是combn 3,因为Var1 * Var2 == Var2 * Var1,而是Var1 * Var2 + Var3! = Var1 * Var3 + Var2.

    这是解决此问题的一种方法:

    library(ISLR)
    data(Auto)
    cols <- names(Auto)[-1]
    res <- expand.grid(combn(cols, 2, simplify=FALSE), cols)
    res <- res[lengths(apply(res, 1, 
        function(x) intersect(as.character(x$Var2), unlist(x$Var1)))) == 0,]
    res <- setNames(data.frame(
        t(data.frame(res$Var1)), as.character(res$Var2), row.names = NULL), 
        paste0("Var", 1:3))
    res$sigma <- apply(res, 1, 
        function(x) sigma(lm(paste0("mpg ~ ", x[1], " * ", x[2], " + ", x[3]), 
            data = Auto)))
    res
    #>             Var1         Var2         Var3    sigma
    #> 1   displacement   horsepower    cylinders 3.930608
    #> 2   displacement       weight    cylinders 4.101442
    #> 3   displacement acceleration    cylinders 4.461698
    #> 4   displacement         year    cylinders 3.733483
    #> 5   displacement       origin    cylinders 4.520063
    #> 6   displacement         name    cylinders 2.422872
    #> 7     horsepower       weight    cylinders 3.931893
    #> 8     horsepower acceleration    cylinders 4.229184
    #> 9     horsepower         year    cylinders 3.650538
    #> 10    horsepower       origin    cylinders 4.169378
    #> 11    horsepower         name    cylinders 2.558382
    #> 12        weight acceleration    cylinders 4.230993
    #> 13        weight         year    cylinders 3.195613
    #> 14        weight       origin    cylinders 4.209175
    #> 15        weight         name    cylinders 2.575035
    #> 16  acceleration         year    cylinders 4.108109
    #> 17  acceleration       origin    cylinders 4.623440
    #> 18  acceleration         name    cylinders 2.752275
    #> 19          year       origin    cylinders 3.957220
    #> 20          year         name    cylinders 2.373485
    #> 21        origin         name    cylinders 2.852769
    #> 22     cylinders   horsepower displacement 3.996131
    #> 23     cylinders       weight displacement 4.144523
    #> 24     cylinders acceleration displacement 4.546081
    #> 25     cylinders         year displacement 3.820275
    #> 26     cylinders       origin displacement 4.586554
    #> 27     cylinders         name displacement 2.824717
    #> 28    horsepower       weight displacement 3.927991
    #> 29    horsepower acceleration displacement 4.098049
    #> 30    horsepower         year displacement 3.511056
    #> 31    horsepower       origin displacement 4.130212
    #> 32    horsepower         name displacement 2.435430
    #> 33        weight acceleration displacement 4.210034
    #> 34        weight         year displacement 3.192645
    #> 35        weight       origin displacement 4.199256
    #> 36        weight         name displacement 2.624553
    #> 37  acceleration         year displacement 3.767955
    #> 38  acceleration       origin displacement 4.391746
    #> 39  acceleration         name displacement 2.763000
    #> 40          year       origin displacement 3.828119
    #> 41          year         name displacement 2.211958
    #> 42        origin         name displacement 2.776667
    #> 43     cylinders displacement   horsepower 4.150379
    #> 44     cylinders       weight   horsepower 4.003118
    #> 45     cylinders acceleration   horsepower 4.361433
    #> 46     cylinders         year   horsepower 3.858321
    #> 47     cylinders       origin   horsepower 4.411365
    #> 48     cylinders         name   horsepower 2.794140
    #> 49  displacement       weight   horsepower 3.952595
    #> 50  displacement acceleration   horsepower 4.144063
    #> 51  displacement         year   horsepower 3.625651
    #> 52  displacement       origin   horsepower 4.391267
    #> 53  displacement         name   horsepower 2.437027
    #> 54        weight acceleration   horsepower 4.141952
    #> 55        weight         year   horsepower 3.160076
    #> 56        weight       origin   horsepower 4.126864
    #> 57        weight         name   horsepower 2.600191
    #> 58  acceleration         year   horsepower 4.048410
    #> 59  acceleration       origin   horsepower 4.322450
    #> 60  acceleration         name   horsepower 2.542587
    #> 61          year       origin   horsepower 3.956825
    #> 62          year         name   horsepower 2.256631
    #> 63        origin         name   horsepower 2.797134
    #> 64     cylinders displacement       weight 4.192360
    #> 65     cylinders   horsepower       weight 3.897318
    #> 66     cylinders acceleration       weight 4.267501
    #> 67     cylinders         year       weight 3.288582
    #> 68     cylinders       origin       weight 4.253179
    #> 69     cylinders         name       weight 2.801128
    #> 70  displacement   horsepower       weight 3.875941
    #> 71  displacement acceleration       weight 4.216553
    #> 72  displacement         year       weight 3.247050
    #> 73  displacement       origin       weight 4.248519
    #> 74  displacement         name       weight 2.410626
    #> 75    horsepower acceleration       weight 4.120773
    #> 76    horsepower         year       weight 3.111929
    #> 77    horsepower       origin       weight 3.994020
    #> 78    horsepower         name       weight 2.491070
    #> 79  acceleration         year       weight 3.282754
    #> 80  acceleration       origin       weight 4.153596
    #> 81  acceleration         name       weight 2.782508
    #> 82          year       origin       weight 3.298971
    #> 83          year         name       weight 1.842066
    #> 84        origin         name       weight 2.766529
    #> 85     cylinders displacement acceleration 4.451150
    #> 86     cylinders   horsepower acceleration 3.964971
    #> 87     cylinders       weight acceleration 4.100272
    #> 88     cylinders         year acceleration 4.135679
    #> 89     cylinders       origin acceleration 4.761837
    #> 90     cylinders         name acceleration 2.890067
    #> 91  displacement   horsepower acceleration 3.848881
    #> 92  displacement       weight acceleration 4.056102
    #> 93  displacement         year acceleration 3.732550
    #> 94  displacement       origin acceleration 4.530853
    #> 95  displacement         name acceleration 2.205730
    #> 96    horsepower       weight acceleration 3.923677
    #> 97    horsepower         year acceleration 3.705777
    #> 98    horsepower       origin acceleration 4.282692
    #> 99    horsepower         name acceleration 2.322535
    #> 100       weight         year acceleration 3.156020
    #> 101       weight       origin acceleration 4.200064
    #> 102       weight         name acceleration 2.633103
    #> 103         year       origin acceleration 5.008915
    #> 104         year         name acceleration 2.494472
    #> 105       origin         name acceleration 2.942866
    #> 106    cylinders displacement         year 3.628036
    #> 107    cylinders   horsepower         year 3.375560
    #> 108    cylinders       weight         year 3.092482
    #> 109    cylinders acceleration         year 4.078887
    #> 110    cylinders       origin         year 4.005163
    #> 111    cylinders         name         year 2.640081
    #> 112 displacement   horsepower         year 3.169038
    #> 113 displacement       weight         year 3.019469
    #> 114 displacement acceleration         year 3.569871
    #> 115 displacement       origin         year 3.743455
    #> 116 displacement         name         year 2.327629
    #> 117   horsepower       weight         year 2.963053
    #> 118   horsepower acceleration         year 3.725775
    #> 119   horsepower       origin         year 3.819683
    #> 120   horsepower         name         year 2.432993
    #> 121       weight acceleration         year 3.223483
    #> 122       weight       origin         year 3.263234
    #> 123       weight         name         year 1.926429
    #> 124 acceleration       origin         year 4.978284
    #> 125 acceleration         name         year 2.719680
    #> 126       origin         name         year 2.659251
    #> 127    cylinders displacement       origin 4.454612
    #> 128    cylinders   horsepower       origin 3.992428
    #> 129    cylinders       weight       origin 4.151961
    #> 130    cylinders acceleration       origin 4.759292
    #> 131    cylinders         year       origin 3.950697
    #> 132    cylinders         name       origin 2.880736
    #> 133 displacement   horsepower       origin 3.916857
    #> 134 displacement       weight       origin 4.099032
    #> 135 displacement acceleration       origin 4.444354
    #> 136 displacement         year       origin 3.679281
    #> 137 displacement         name       origin 2.419996
    #> 138   horsepower       weight       origin 3.889403
    #> 139   horsepower acceleration       origin 4.197468
    #> 140   horsepower         year       origin 3.560784
    #> 141   horsepower         name       origin 2.547681
    #> 142       weight acceleration       origin 4.194124
    #> 143       weight         year       origin 3.140978
    #> 144       weight         name       origin 2.611288
    #> 145 acceleration         year       origin 5.012415
    #> 146 acceleration         name       origin 2.956412
    #> 147         year         name       origin 2.473915
    #> 148    cylinders displacement         name 2.733227
    #> 149    cylinders   horsepower         name 2.652409
    #> 150    cylinders       weight         name 2.717629
    #> 151    cylinders acceleration         name 2.869345
    #> 152    cylinders         year         name 2.580032
    #> 153    cylinders       origin         name 2.852769
    #> 154 displacement   horsepower         name 2.628840
    #> 155 displacement       weight         name 2.698532
    #> 156 displacement acceleration         name 2.708595
    #> 157 displacement         year         name 2.406705
    #> 158 displacement       origin         name 2.719861
    #> 159   horsepower       weight         name 2.648480
    #> 160   horsepower acceleration         name 2.555832
    #> 161   horsepower         year         name 2.453807
    #> 162   horsepower       origin         name 2.682554
    #> 163       weight acceleration         name 2.797606
    #> 164       weight         year         name 2.294686
    #> 165       weight       origin         name 2.738440
    #> 166 acceleration         year         name 2.662019
    #> 167 acceleration       origin         name 2.899071
    #> 168         year       origin         name 2.630942
    

    reprex package (v0.3.0) 于 2020 年 6 月 14 日创建

    【讨论】:

    • 非常感谢,您是如何发现这正是我想要的?你做的桌子做工很好,很漂亮。再次感谢
    • 对我来说什么都没有出现,你知道为什么吗?
    • 这是用reprex 包制作的,它应该使其可重现而无需任何额外的依赖。您是否在复制示例时遇到问题,或者您自己的数据有问题?
    猜你喜欢
    • 2017-07-22
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多