【问题标题】:R: divide values in rows dataframe by the maximum value in that rowR:将行数据框中的值除以该行中的最大值
【发布时间】:2020-11-16 23:36:47
【问题描述】:

我想在 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 解决方案。谁能指出我正确的方向以获得更好的解决方案?

【问题讨论】:

    标签: r dataframe max row


    【解决方案1】:

    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)))
    

    【讨论】:

      【解决方案2】:

      使用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
      

      【讨论】:

        猜你喜欢
        • 2014-04-06
        • 2020-12-23
        • 2012-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        相关资源
        最近更新 更多