Home > Blockchain >  R - transform quotient by the divisor to a summation
R - transform quotient by the divisor to a summation

Time:01-30

There is a list of weighted values. Integers in a second list are used for the weighting. The values are divided by the integers.

I am struggling with (also because I do not know the correct mathematical term or function term in r) the transformation of the resulting quotients. These should be transformed, so a summation of the quotient appears, which is determined by the divisor/specific integer.

values <- c(100, 101, 102, 103, 316, 330, 106, 205)
> values
[1] 100 101 102 103 316 330 106 205

vectors <- c(1, 1, 1, 1, 3, 3, 1, 2)
> vectors
[1] 1 1 1 1 3 3 1 2


The correct result of a division of the values by the integers is indeed:

> values/vectors
[1] 100.0000 101.0000 102.0000 103.0000 105.3333 110.0000 106.0000 102.5000

However, I would like to transform the result by taking the integers into account, so that in the end the following result would appear:

[1] 100.0000 101.0000 102.0000 103.0000 105.3333 105.3333 105.3333 110.0000 110.0000 110.0000 106.0000 102.5000 102.5000

Thank you.

CodePudding user response:

I think you're looking for the rep() function:

values <- c(100, 101, 102, 103, 316, 330, 106, 205)
vectors <- c(1, 1, 1, 1, 3, 3, 1, 2)

> rep(values / vectors, times = vectors)
 [1] 100.0000 101.0000 102.0000 103.0000 105.3333 105.3333 105.3333 110.0000 110.0000 110.0000
[11] 106.0000 102.5000 102.5000
  •  Tags:  
  • Related