用R中的列总和除以数据集中的每个单元格 [英] Dividing each cell in a data set by the column sum in R

查看:574
本文介绍了用R中的列总和除以数据集中的每个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据帧中的每个单元格除以该列的总和。例如,我有一个数据框df:

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    5    4

我想创建一个新的数据框,将每个单元格取入并除以和

I would like to create a new data frame that takes each cell in and divides by the sum of the column, like so:

sample   a   b   c
a2      .167  .444  .6
a3      .833  .556  .4

我已经看到了使用sweep()的答案,但这看起来像是用于矩阵的,并且我有数据框。我了解如何使用colSums(),但不确定如何编写一个遍历列中每个单元格然后除以列总和的函数。谢谢您的帮助!

I have seen answers using sweep(), but that looks like its for matrices, and I have data frames. I understand how to use colSums(), but I'm not sure how to write a function that loops through every cell in the column, and then divides by the column sum. Thanks for the help!

推荐答案

为此:

> d = data.frame(sample=c("a2","a3"),a=c(1,5),b=c(4,5),c=c(6,4))
> d
  sample a b c
1     a2 1 4 6
2     a3 5 5 4

您可以通过应用其余的列来替换除第一列以外的所有列:

You can replace every column other than the first by applying over the rest:

> d[,-1] = apply(d[,-1],2,function(x){x/sum(x)})

> d
  sample         a         b   c
1     a2 0.1666667 0.4444444 0.6
2     a3 0.8333333 0.5555556 0.4

如果您不想踩到 d ,请事先制作副本。

If you don't want d being stomped on make a copy beforehand.

这篇关于用R中的列总和除以数据集中的每个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆