Divide each data frame row by vector in R

前端 未结 5 788
逝去的感伤
逝去的感伤 2020-12-15 18:54

I\'m trying to divide each number within a data frame with 16 columns by a specific number for each column. The numbers are stored as a data frame with 1-16 corresponding to

5条回答
  •  北海茫月
    2020-12-15 19:28

    Suppose we have a dataframe, df:

    > df
      a b   c
    1 1 3 100
    2 2 4 110
    

    And we want to divide through each row by the same vector, vec:

    > vec <- df[1,]
    > vec
      a b   c
    1 1 3 100
    

    Then we can use mapply as follows:

    > mapply('/', df, vec)
         a        b   c
    [1,] 1 1.000000 1.0
    [2,] 2 1.333333 1.1
    

提交回复
热议问题