通过将所有预先存在的变量除以所有其他变量来创建新变量

Create new variables by dividing all pre-exisiting variables by all other variables

我想通过将所有预先存在的变量彼此相除来创建新变量

例如 X1/X1、X1/X2、X1/X3、X1/X4、X1/X5、X1/X6、X1/X7、X1/X8、X1/X9, X1/X10, X2/X1、X2/X2、X2/X3、X2/X4、X2/X5、X2/X6、X2/X7、X2/X8、X2/X9, X2/X10, X3/X1、X3/X2 ...

我开始尝试单独执行每个操作,如下所示,但我需要用多个变量名复制它,因此自动化(我假设 function/lapply)将是理想的。

ds$rom_3_5m <- (ds$roll_open_mean_3m/ds$roll_open_mean_5m)
ds$rom_3_10m <- (ds$roll_open_mean_3m/ds$roll_open_mean_10m)
ds$rom_3_15m <- (ds$roll_open_mean_3m/ds$roll_open_mean_15m)
ds$rom_3_30m <- (ds$roll_open_mean_3m/ds$roll_open_mean_30m)
ds$rom_3_60m <- (ds$roll_open_mean_3m/ds$roll_open_mean_60m)
ds$rom_3_120m <- (ds$roll_open_mean_3m/ds$roll_open_mean_120m)
ds$rom_3_240m <- (ds$roll_open_mean_3m/ds$roll_open_mean_240m)
ds$rom_3_480m <- (ds$roll_open_mean_3m/ds$roll_open_mean_480m)
ds$rom_3_960m <- (ds$roll_open_mean_3m/ds$roll_open_mean_960m)
ds$rom_3_1920m <- (ds$roll_open_mean_3m/ds$roll_open_mean_1920m)
ds$rom_3_3840m <- (ds$roll_open_mean_3m/ds$roll_open_mean_3840m)
ds$rom_3_7680m <- (ds$roll_open_mean_3m/ds$roll_open_mean_7680m)
ds$rom_3_15360m <- (ds$roll_open_mean_3m/ds$roll_open_mean_15360m)
ds$rom_3_30720m <- (ds$roll_open_mean_3m/ds$roll_open_mean_30720m)
ds$rom_3_61440m <- (ds$roll_open_mean_3m/ds$roll_open_mean_61440m)
ds$rom_3_122880m <- (ds$roll_open_mean_3m/ds$roll_open_mean_122880m)
ds$rom_3_245760m <- (ds$roll_open_mean_3m/ds$roll_open_mean_245760m)
ds$rom_3_491520m <- (ds$roll_open_mean_3m/ds$roll_open_mean_491520m)

#5m
ds$rom_5_3m <- (ds$roll_open_mean_5m/ds$roll_open_mean_3m)
ds$rom_5_10m <- (ds$roll_open_mean_5m/ds$roll_open_mean_10m)
ds$rom_5_15m <- (ds$roll_open_mean_5m/ds$roll_open_mean_15m)
ds$rom_5_30m <- (ds$roll_open_mean_5m/ds$roll_open_mean_30m)
ds$rom_5_60m <- (ds$roll_open_mean_5m/ds$roll_open_mean_60m)
ds$rom_5_120m <- (ds$roll_open_mean_5m/ds$roll_open_mean_120m)
ds$rom_5_240m <- (ds$roll_open_mean_5m/ds$roll_open_mean_240m)
ds$rom_5_480m <- (ds$roll_open_mean_5m/ds$roll_open_mean_480m)
ds$rom_5_960m <- (ds$roll_open_mean_5m/ds$roll_open_mean_960m)
ds$rom_5_1920m <- (ds$roll_open_mean_5m/ds$roll_open_mean_1920m)
ds$rom_5_3840m <- (ds$roll_open_mean_5m/ds$roll_open_mean_3840m)
ds$rom_5_7680m <- (ds$roll_open_mean_5m/ds$roll_open_mean_7680m)
ds$rom_5_15360m <- (ds$roll_open_mean_5m/ds$roll_open_mean_15360m)
ds$rom_5_30720m <- (ds$roll_open_mean_5m/ds$roll_open_mean_30720m)
ds$rom_5_61440m <- (ds$roll_open_mean_5m/ds$roll_open_mean_61440m)
ds$rom_5_122880m <- (ds$roll_open_mean_5m/ds$roll_open_mean_122880m)
ds$rom_5_245760m <- (ds$roll_open_mean_5m/ds$roll_open_mean_245760m)
ds$rom_5_491520m <- (ds$roll_open_mean_5m/ds$roll_open_mean_491520m)

#10m
ds$rom_10_3m <- (ds$roll_open_mean_10m/ds$roll_open_mean_3m)
ds$rom_10_5m <- (ds$roll_open_mean_10m/ds$roll_open_mean_5m)
ds$rom_10_15m <- (ds$roll_open_mean_10m/ds$roll_open_mean_15m)

我有一个包含 40 多个变量和 600 万行的数据框,我在下面附上了一个较小的示例数据框。

提前致谢!

查理

X1  X2 X3  X4 X5  X6 X7 X8 X9 X10
1  57  77 48   8 31  43 47 13 26  88
2  25  75 86  77  4  65  5 49 31  57
3  91  90 42  69 82  33 56 99 47  39
4  35  96 86  77 67  77 20 17 77  92
5   6 100 50  62 16  31  0 39 72   4
6  90  34 74  89 71  37 73 45 24  28
7  24  22 92  13 57  97 32  2 12  80
8  74  59 49   2 97 100 15 37 15  67
9  43  38 66  97  8  20 85 25 97  67
10 82   4 56  40 42  46 44 98 98  76
11 60  68 92  99 81  92 78 59 23  81
12 22  57 37 100  7   1 89 41 40  56
13 69  13  1  82 89  45 83 24 71  29
14  8  14 66  48 94   8 20  3 28  63
15 26  70 56  62  9  34 11 86 71  64
16  7  55 15 100 91  89 46 74 98  14
17 29  68 19  66 83  29 84 76 90  45
18 27  76  6  48 17  28  8  7 52  37
19 68  58 51  75 60  57 74 46 98  93
20 15  15 89  55 23   3  3  8 32  37
21 78  49 57  48 96  89  4 95 67  58
22 12  36 42  59 27  92 48  0 92  28
23 51  17 77  61 84  53 46 22 27  36
24 40  84 83  35 19  13 80 78 96  87
25 44  80 25  72 43  17 74 70 52  36
26 14  61 63  82 16  47 32 93 19  84
27 93  19 28  62 74   1 85 65 50   9
28 80  62  6  58 48  97 97 18 65  43
29 12  58 95  79 37  89 89 83 22  85
30 57  73 22  88 99  63 58 87 90  66

正如@27 ϕ 9 在评论中建议的那样,您应该使用 lapply 解决方案。

这样,您还可以创建一个具有正确名称的唯一数据框

l <- lapply(df, `/`, df)
l <- unlist(l, recursive = FALSE)
data.frame(l)