Doug
Doug

Reputation: 597

Efficient multiplication of columns in a data frame

I have a large data frame in which I am multiplying two columns together to get another column. At first I was running a for-loop, like so:

for(i in 1:nrow(df)){
    df$new_column[i] <- df$column1[i] * df$column2[i]
}

but this takes like 9 days.

Another alternative was plyr, and I actually might be using the variables incorrectly:

new_df <- ddply(df, .(column1,column2), transform, new_column = column1 * column2)

but this is taking forever

Upvotes: 12

Views: 35615

Answers (5)

Stefano Verugi
Stefano Verugi

Reputation: 191

this works with 2 or more numeric columns in a dataframe

 df$product <- apply(df,1,prod)

Upvotes: 1

Ali Safari
Ali Safari

Reputation: 1775

You can simply create a function to handle all sort of multiplications like this on:

GetMultiplication <- function(x,y) {
x *y
}

# for example:
xCol<-c(1,2,3,4,5)
yCol<-c(10,20,30,40,50)
const = 0.055

#Case 1: Column 1 * Column 2
ZCol_1 <- GetMultiplication (xCol,yCol)
print(ZCol_1)
#> [1]  10  40  90 160 250

#Case 2: Column 1 * (Column 1 * 10 + 1000)
ZCol_2 <- GetMultiplication (xCol,xCol*10 + 1000)
print(ZCol_2)
#> [1] 1010 2040 3090 4160 5250

#Case 3: Column 1 * a constant value
ZCol_3 <- GetMultiplication (xCol,const)
print(ZCol_3)
#> [1] 0.055 0.110 0.165 0.220 0.275

Upvotes: 1

mnel
mnel

Reputation: 115392

A data.table solution will avoid lots of internal copying while having the advantages of not spattering the code with $.

 library(data.table)
 DT <- data.table(df)
 DT[ , new := column1 * column2]

Upvotes: 12

Gavin Simpson
Gavin Simpson

Reputation: 174813

A minor, somewhat less efficient, version of Sacha's Answer is to use transform() or within()

df <- transform(df, new = column1 * column2)

or

df <- within(df, new <- column1 * column2)

(I hate spattering my user code with $.)

Upvotes: 11

Sacha Epskamp
Sacha Epskamp

Reputation: 47551

As Blue Magister said in comments,

df$new_column <- df$column1 * df$column2

should work just fine. Of course we can never know for sure if we don't have an example of the data.

Upvotes: 26

Related Questions