Multiplying all columns in dataframe by single column

前端 未结 5 613
情书的邮戳
情书的邮戳 2020-12-21 02:10

I know it is a basic quaestion but couldnt find any solution to it. I want to multiply all columns of a dataframe by single column.

df1<-data.frame(F1=c(1         


        
5条回答
  •  暖寄归人
    2020-12-21 02:26

    We can vectorize the multiplication by replicating the 'C' column

    df1 * C[row(df1)]
    #     F1   F2   F3
    #1  2.0  2.0  2.0
    #2  5.0  5.0  5.0
    #3 16.0 16.0 16.0
    #4  4.5  4.5  4.5
    

    Or use the rep explicitly

    df1 * rep(C$C, ncol(df1))
    

提交回复
热议问题