Search code examples
rdataframepermute

R. Compute product of value in row by permutated value of other column n times


I have a data frame like this (1000000 rows):

A    B    C
a    0.2  4 
b    0.8  7
c    1    8
d    0.2  1
e    0.6  9

I would like to multiply each value in B by a random number taken from C (permutation without replacement) producing a new value x for each row. Then I want to sum all x values obtaining a new row y. Repeat this n times. I don't need the xn columns in my data frame, just a vector with the yn values.

I would get something like this (2 iterations):

A    B    C    x1           x2        .... xn
a    0.2  4    0.2*1=0.2    0.2*4=0.8
b    0.8  7    0.8*8=6.4    0.8*9=0.72
c    1    8    1*7=7        1*1=1
d    0.2  1    0.2*9=1.8    0.2*7=1.4
e    0.6  9    0.6*4=2.4    0.6*8=4.8
y              17.8         8.72      .... yn

Solution

  • Using replicate helps here:

    n <- 10
    (y <- with(df, replicate(sum(B * sample(C)), n = n)))
    # [1] 16.4 16.4 18.0 17.8 14.2 14.2 18.0 20.4 15.2 19.8
    

    If sampling with replacement were an option, it could be made faster by generating a single large matrix of values of C and then using colSums.