首页 > 解决方案 > 计算 R 中的所有列

问题描述

我有一个 data.frame 如下。对于每一列,如果值在 2.5% (>=) [倒数第二列] 和 97.5% (<=) [最后一列] 之间的范围内,我想分配一个值“1”。如果不是,那么我想分配一个值 2。在有 NA 的地方,我想让 NA 保持原样。

> df
              S1    S2    S3    S4    S5    S6    2.5%  97.5%
Gene1         0.02  0.04  0.05  0.03  0.10  0.06  0.01  0.08
Gene1         0.04  0.04  0.04  0.06  0.03  0.04  0.03  0.09
Gene1        51.00 57.00  50.00 54.00 70.00 63.00 41.00 71.00
Gene1         0.46  0.35  0.28  0.41  0.26  0.29  0.21  0.45
Gene1         0.09  0.08  0.09  0.09  0.08  0.09  0.07  0.10
Gene1        46.80 44.60  48.40 45.30 40.90 46.10 36.69 49.20
Gene1           NA    NA  20.30 14.10 12.10 15.50  8.72 25.61
Gene1         1.96  1.05  1.39  1.56  1.54  1.71  1.24  2.00
Gene1         0.53  0.52  0.61  0.83  0.73  0.37  0.36  0.91
Gene1         1.05  0.55  0.85  1.30  1.14  0.64  0.61  1.39
Gene1        22.31 28.88  26.75 25.08 26.29 23.34 19.76 33.44

所以最后,我想要一个看起来像这样的data.frame。

> df_updated
              S1  S2  S3  S4  S5  S6
Gene1         1   1   1   1   2   1
Gene1         1   1   1   1   1   1
Gene1         1   1   1   1   1   1
Gene1         2   1   1   1   1   1
Gene1         1   1   1   1   1   1
Gene1         1   1   1   1   1   1
Gene1        NA  NA   1   1   1   1
Gene1         1   2   1   1   1   1
Gene1         1   1   1   1   1   1
Gene1         2   2   1   1   1   1
Gene1         1   1   1   1   1   1

我尝试了以下类似的方法,但最终出现错误。我从这里了解到(if/while 中的错误(条件){:需要 TRUE/FALSE 的缺失值)这与我在矩阵中拥有的 NA 有关,但我不确定如何通过代码来适应它,并获得我想要的数据框'df_updated'。

df_updated <- as.data.frame(lapply(df, function(x) if (x>=df$`2.5%` & x<=df$`97.5%`) {x==1} else {x==2}))

Error in if (x >= df$`2.5%` & x <= df$`97.5%`) { : 
  missing value where TRUE/FALSE needed
In addition: There were 50 or more warnings (use warnings() to see the first 50)

任何帮助表示赞赏。谢谢你。

标签: rmultiple-columnsna

解决方案


要测试您的值是否在分位数范围内,您可以ifelse在内部使用,apply例如:

df <- read.table(header=TRUE, text="
x             S1    S2    S3    S4    S5    S6    x2.5  x97.5
Gene1         0.02  0.04  0.05  0.03  0.10  0.06  0.01  0.08
Gene1         0.04  0.04  0.04  0.06  0.03  0.04  0.03  0.09
Gene1        51.00 57.00  50.00 54.00 70.00 63.00 41.00 71.00
Gene1         0.46  0.35  0.28  0.41  0.26  0.29  0.21  0.45
Gene1         0.09  0.08  0.09  0.09  0.08  0.09  0.07  0.10
Gene1        46.80 44.60  48.40 45.30 40.90 46.10 36.69 49.20
Gene1           NA    NA  20.30 14.10 12.10 15.50  8.72 25.61
Gene1         1.96  1.05  1.39  1.56  1.54  1.71  1.24  2.00
Gene1         0.53  0.52  0.61  0.83  0.73  0.37  0.36  0.91
Gene1         1.05  0.55  0.85  1.30  1.14  0.64  0.61  1.39
Gene1        22.31 28.88  26.75 25.08 26.29 23.34 19.76 33.44")

t(apply(df[-1], 1, function(x) ifelse(x>=x[length(x)-1] & x<=x[length(x)], 1, 2)))[,1:6]
#      S1 S2 S3 S4 S5 S6
# [1,]  1  1  1  1  2  1
# [2,]  1  1  1  1  1  1
# [3,]  1  1  1  1  1  1
# [4,]  2  1  1  1  1  1
# [5,]  1  1  1  1  1  1
# [6,]  1  1  1  1  1  1
# [7,] NA NA  1  1  1  1
# [8,]  1  2  1  1  1  1
# [9,]  1  1  1  1  1  1
#[10,]  1  2  1  1  1  1
#[11,]  1  1  1  1  1  1

推荐阅读