Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient multiplication of columns in a data frame

Tags:

dataframe

r

plyr

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

like image 876
Doug Avatar asked Sep 10 '12 18:09

Doug


People also ask

How do you multiply values in a column in a data frame?

Use the * operator to multiply a column by a constant number Select a column of DataFrame df using syntax df["column_name"] and set it equal to n * df["column_name"] where n is the number to multiply by.

How do I multiply a column in a DataFrame in R?

For this task, we can use the $ and * operators as shown below. The $ operator extracts the values of the column that we want to multiply, and the * operator telly R that we want to multiply the values before the * sign by the values after the * sign.

Can you multiply Dataframes?

The mul() method multiplies each value in the DataFrame with a specified value. The specified value must be an object that can be multiplied with the values of the DataFrame.


1 Answers

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.

like image 171
Sacha Epskamp Avatar answered Oct 21 '22 15:10

Sacha Epskamp