Efficient multiplication of columns in a data frame

后端 未结 4 1473
南方客
南方客 2020-12-09 20:56

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(d         


        
4条回答
  •  失恋的感觉
    2020-12-09 21:38

    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]
    

提交回复
热议问题