Efficient multiplication of columns in a data frame

后端 未结 4 1451
南方客
南方客 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:18

    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 $.)

    0 讨论(0)
  • 2020-12-09 21:21

    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.

    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题