Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying column value by another value depending on value in certain column R

Tags:

r

dplyr

rows

in the following dataset, I would like to multiply value in column Size by value in column Month1, Month2 or Month3 depending on what number we have in column Month. So if in certain row the Month value is 2, I would like to multiply the value in column Size by the value in column Month2 and save the result in new column NewSize. Many thanks for your help in advance!

Orig = c("A","B","A","A","B","A","A","B","A")
Dest = c("B","A","C","B","A","C","B","A","C")
Month = c(1,1,1,2,2,2,3,3,3)
Size = c(30,20,10,10,20,20,30,50,20)
Month1 = c(1,0.2,0,1,0.2,0,1,0.2,0)
Month2 = c(0.6,1,0,0.6,1,0,0.6,1,0)
Month3 = c(0,1,0.6,0,1,0.6,0,1,0.6)
df <- data.frame(Orig,Dest,Month,Size,Month1,Month2,Month3)
df

  Orig Dest Month Size Month1 Month2 Month3
1    A    B     1   30    1.0    0.6    0.0
2    B    A     1   20    0.2    1.0    1.0
3    A    C     1   10    0.0    0.0    0.6
4    A    B     2   10    1.0    0.6    0.0
5    B    A     2   20    0.2    1.0    1.0
6    A    C     2   20    0.0    0.0    0.6
7    A    B     3   30    1.0    0.6    0.0
8    B    A     3   50    0.2    1.0    1.0
9    A    C     3   20    0.0    0.0    0.6
like image 954
MIH Avatar asked Apr 17 '15 21:04

MIH


People also ask

How do I multiply a column by another column in R?

To multiply only one column with a number, we can simply use the multiplication operator * but need to replace the original column with the new values.

How do you multiply values in R?

In R the asterisk (*) is used for element-wise multiplication. This is where the elements in the same row are multiplied by one another. We can see that the output of c*x and x*c are the same, and the vector x doubles matrix c. In R percent signs combined with asterisks are used for matrix multiplication (%*%).

How do I multiply all rows in R?

To multiply a rows or columns of a matrix, we need to use %*% symbol that perform the multiplication for matrices in R. If we have a matrix M with 5 rows and 5 columns then row 1 of M can be multiplied with column 1 of M using M[1,]%*%M[,1], similarly, we can multiply other rows and columns.


1 Answers

Here's one alternative using ifelse

> transform(df, NewSize=ifelse(Month==1, Size*Month1, 
                  ifelse(Month==2, Size*Month2, Size*Month3)))
  Orig Dest Month Size Month1 Month2 Month3 NewSize
1    A    B     1   30    1.0    0.6    0.0      30
2    B    A     1   20    0.2    1.0    1.0       4
3    A    C     1   10    0.0    0.0    0.6       0
4    A    B     2   10    1.0    0.6    0.0       6
5    B    A     2   20    0.2    1.0    1.0      20
6    A    C     2   20    0.0    0.0    0.6       0
7    A    B     3   30    1.0    0.6    0.0       0
8    B    A     3   50    0.2    1.0    1.0      50
9    A    C     3   20    0.0    0.0    0.6      12
like image 128
Jilber Urbina Avatar answered Nov 15 '22 09:11

Jilber Urbina