将数据帧中的所有数字除以同一数据帧中的所选行和R中的相应列位置

时间:2014-01-23 16:24:14

标签: r dataframe divide

我有一个数据框x1,我用这段代码生成了<{p}}

x <- c(1:10)
y <- x^3
z <- y-20
s <- z/3
t <- s*6
q <- s*y
x1 <- cbind(x,y,z,s,t,q)
x1 <- data.frame(x1)

    x    y   z          s    t             q
1   1    1 -19  -6.333333  -38     -6.333333
2   2    8 -12  -4.000000  -24    -32.000000
3   3   27   7   2.333333   14     63.000000
4   4   64  44  14.666667   88    938.666667
5   5  125 105  35.000000  210   4375.000000
6   6  216 196  65.333333  392  14112.000000
7   7  343 323 107.666667  646  36929.666667
8   8  512 492 164.000000  984  83968.000000
9   9  729 709 236.333333 1418 172287.000000
10 10 1000 980 326.666667 1960 326666.666667

现在我想将此数据框x1与同一数据帧的第一行分开,因此我使用以下代码行,如上所述here

x2 <- x1[1,]
require(stats)
sweep(x1, 2, x2, `/`)

所以数据框x2看起来像这样,

  x y   z         s   t         q
1 1 1 -19 -6.333333 -38 -6.333333

我需要的结果是数据框中的每个数字应除以x2中与其自己的列位置对应的数字。例如,当数据框x1的第3行除以x2时,结果应为3 27 -0.368421 -0.368421 -0.368421 -9.947336但我得到Error in Ops.data.frame(x, aperm(array(STATS, dims[perm])

有人可以指出我需要改变什么以获得所需的结果。

3 个答案:

答案 0 :(得分:5)

问题是由于x2是一行数据帧而不是矢量。将其转换为unlist的向量,它将起作用:

x2 <- x1[1,]
sweep(x1, 2, unlist(x2), `/`)

    x    y           z           s           t             q
1   1    1   1.0000000   1.0000000   1.0000000      1.000000
2   2    8   0.6315789   0.6315789   0.6315789      5.052632
3   3   27  -0.3684211  -0.3684211  -0.3684211     -9.947368
4   4   64  -2.3157895  -2.3157895  -2.3157895   -148.210526
5   5  125  -5.5263158  -5.5263158  -5.5263158   -690.789474
6   6  216 -10.3157895 -10.3157895 -10.3157895  -2228.210526
7   7  343 -17.0000000 -17.0000000 -17.0000000  -5831.000000
8   8  512 -25.8947368 -25.8947368 -25.8947368 -13258.105263
9   9  729 -37.3157895 -37.3157895 -37.3157895 -27203.210526
10 10 1000 -51.5789474 -51.5789474 -51.5789474 -51578.947368

答案 1 :(得分:4)

你也可以尝试这样的事情:

x1 / x1[1, , drop = TRUE]
#     x    y           z           s           t             q
# 1   1    1   1.0000000   1.0000000   1.0000000      1.000000
# 2   2    8   0.6315789   0.6315789   0.6315789      5.052632
# 3   3   27  -0.3684211  -0.3684211  -0.3684211     -9.947368
# 4   4   64  -2.3157895  -2.3157895  -2.3157895   -148.210526
# 5   5  125  -5.5263158  -5.5263158  -5.5263158   -690.789474
# 6   6  216 -10.3157895 -10.3157895 -10.3157895  -2228.210526
# 7   7  343 -17.0000000 -17.0000000 -17.0000000  -5831.000000
# 8   8  512 -25.8947368 -25.8947368 -25.8947368 -13258.105263
# 9   9  729 -37.3157895 -37.3157895 -37.3157895 -27203.210526
# 10 10 1000 -51.5789474 -51.5789474 -51.5789474 -51578.947368

答案 2 :(得分:2)

将data.frame转换为矩阵(因为您只有数字数据):

x1 <- as.matrix(x1)

t(t(x1)/x1[1,])

       x    y           z           s           t             q
 [1,]  1    1   1.0000000   1.0000000   1.0000000      1.000000
 [2,]  2    8   0.6315789   0.6315789   0.6315789      5.052632
 [3,]  3   27  -0.3684211  -0.3684211  -0.3684211     -9.947368
 [4,]  4   64  -2.3157895  -2.3157895  -2.3157895   -148.210526
 [5,]  5  125  -5.5263158  -5.5263158  -5.5263158   -690.789474
 [6,]  6  216 -10.3157895 -10.3157895 -10.3157895  -2228.210526
 [7,]  7  343 -17.0000000 -17.0000000 -17.0000000  -5831.000000
 [8,]  8  512 -25.8947368 -25.8947368 -25.8947368 -13258.105263
 [9,]  9  729 -37.3157895 -37.3157895 -37.3157895 -27203.210526
[10,] 10 1000 -51.5789474 -51.5789474 -51.5789474 -51578.947368