R:将行数据框中的值除以该行中的最大值

R: divide values in rows dataframe by the maximum value in that row

我想在 R 中做一件简单的事情。我有一个包含 1000 行和几列的数据框,我想将数据框中的每个值除以相应行中的最大值。

数据帧示例:

x <- data.frame("condition_1" = c(2,4,6,8,10), "condition_2" = c(1,4,5,3,2), "condition_3" = c(1,5,9,3,12))
row.names(x) <- c('gene_1', 'gene_2', 'gene_3', 'gene_4', 'gene_5')

数据框如下所示:

> x
       condition_1 condition_2 condition_3
gene_1           2           1           1
gene_2           4           4           5
gene_3           6           5           9
gene_4           8           3           3
gene_5          10           2          12

现在,我用下面的代码解决了我的问题:

xnorm = NULL
for (row in 1:nrow(x)){
  tmp = x[row,] / max(x[row,]) 
  xnorm = rbind(xnorm, tmp)
}
rownames(xnorm) = rownames(x)

输出如下所示:

> xnorm
       condition_1 condition_2 condition_3
gene_1   1.0000000   0.5000000       0.500
gene_2   0.8000000   0.8000000       1.000
gene_3   0.6666667   0.5555556       1.000
gene_4   1.0000000   0.3750000       0.375
gene_5   0.8333333   0.1666667       1.000

如您所见,这有效。但是,我的解决方案似乎过于复杂,而且我确信必须有一些干净的 R 解决方案。谁能指出我正确的方向以获得更好的解决方案?

base R中可以用vectorizedpmax

来完成
x/do.call(pmax, x)

-输出

#       condition_1 condition_2 condition_3
#gene_1   1.0000000   0.5000000       0.500
#gene_2   0.8000000   0.8000000       1.000
#gene_3   0.6666667   0.5555556       1.000
#gene_4   1.0000000   0.3750000       0.375
#gene_5   0.8333333   0.1666667       1.000

或使用效率较低的方法 apply

t(apply(x, 1, function(u) u/max(u)))

使用tidyverse:

library(tidyverse)
#Code
newx <- x %>% rownames_to_column('id') %>%
  pivot_longer(-id) %>%
  group_by(id) %>% mutate(value=value/max(value,na.rm=T)) %>%
  pivot_wider(names_from=name,values_from=value) %>%
  column_to_rownames('id')

输出:

       condition_1 condition_2 condition_3
gene_1   1.0000000   0.5000000       0.500
gene_2   0.8000000   0.8000000       1.000
gene_3   0.6666667   0.5555556       1.000
gene_4   1.0000000   0.3750000       0.375
gene_5   0.8333333   0.1666667       1.000