数据帧中列的有效乘法 [英] Efficient multiplication of columns in a data frame

查看:60
本文介绍了数据帧中列的有效乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数据框,其中我将两列相乘以获得另一列.最初,我正在运行一个for循环,如下所示:

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]
}

但这大约需要9天.

另一种选择是plyr,实际上我可能错误地使用了变量:

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

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

但这会永远存在

推荐答案

正如Blue Magister在评论中所说,

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.

这篇关于数据帧中列的有效乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆