Dividing all the numbers in a data frame by a chosen row in the same data frame and corresponding column position in R

StackOverflow https://stackoverflow.com/questions/21313812

  •  01-10-2022
  •  | 
  •  

Question

I have a data frame x1 which I generated with this code,

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

Now I want to divide this data frame x1 with the first row of the same data frame, so I use these following lines of code, as described here

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

So the data frame x2 looks like this,

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

The outcome which I need is each of the number in the data frame should be divided by number in x2 corresponding to its own column position. For example when the row 3 of the data frame x1 is divided by x2 the result should be 3 27 -0.368421 -0.368421 -0.368421 -9.947336 But I am getting an Error in Ops.data.frame(x, aperm(array(STATS, dims[perm])

Could some one indicate what I need to change to get the desired result.

Was it helpful?

Solution 2

You can also try something like this:

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

OTHER TIPS

The problem is due to x2 being a one-row data frame and not a vector. Transform it to a vector with unlist and it will work:

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

Turn your data.frame into a matrix (since you only have only numeric data):

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top