Dividing each cell in a data set by the column sum in R

后端 未结 5 960
遇见更好的自我
遇见更好的自我 2020-12-17 01:35

I am trying to divide each cell in a data frame by the sum of the column. For example, I have a data frame df:

sample   a   b   c
a2      1    4    6
a3             


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-17 02:17

    You can use the transpose of the matrix and then transpose again:

    t(t(as.matrix(df))/colSums(df))
    

提交回复
热议问题