R - 通过减法来删除重复的行

时间:2018-01-24 18:24:59

标签: r

我有一个大的基因表达数据框,其中有重复的基因代表两组(对照和疾病)。对于重复的基因,我需要只选择一个,基于对照组的平均值减去疾病组的平均值(同一行)。在这种情况下,我必须选择具有最大减法结果的基因。

以下是我的数据框的一个小例子:

 GENES=c("7A5", "A1BG", "A1BG", "A1BG","AAAS","AAAS", "AFDS","AFDS","AFDS")
 Control1 = c(2.1471840, -0.9092227, -1.4875100, -2.79559765,  0.05143231, -1.25764808,  0.6104962,  0.09226673, -0.8037355)
 Control2 = c(-0.3709474,  1.4587290,  1.4545832, -0.27379895, -0.45116476,  1.56286706, -0.9225275, -0.54779659, -1.0586287)
 Control3 = c(-1.1321667, -1.3051079, -0.9658358, -0.05914144, -0.20133056,  0.03029207,  1.0015907,  1.18145151,  0.5360956)
 Disease1 = c(0.6824169,  0.1645328,  2.6276603,  1.11739548, -1.13592005, -0.12666909, -0.4667365, -0.80153098, -1.1085319)
 Disease2 = c(1.1014914, -1.4461279,  1.0965057, -1.58379531, -0.12457328,  0.59232328,  0.2319656,  0.46981373, -0.4540254)

df=data.frame(GENES,Control1,Control2,Control3,Disease1,Disease2)

> df
  GENES    Control1   Control2    Control3   Disease1   Disease2
1   7A5  2.14718400 -0.3709474 -1.13216670  0.6824169  1.1014914
2  A1BG -0.90922270  1.4587290 -1.30510790  0.1645328 -1.4461279
3  A1BG -1.48751000  1.4545832 -0.96583580  2.6276603  1.0965057
4  A1BG -2.79559765 -0.2737989 -0.05914144  1.1173955 -1.5837953
5  AAAS  0.05143231 -0.4511648 -0.20133056 -1.1359200 -0.1245733
6  AAAS -1.25764808  1.5628671  0.03029207 -0.1266691  0.5923233
7  AFDS  0.61049620 -0.9225275  1.00159070 -0.4667365  0.2319656
8  AFDS  0.09226673 -0.5477966  1.18145151 -0.8015310  0.4698137
9  AFDS -0.80373550 -1.0586287  0.53609560 -1.1085319 -0.4540254

换句话说,如果我们使用基因A1BG作为例子,第3行,我需要:

mean(col2,col3,col4) - mean(col5,col6) = ?

对第4行和第5行(A1BG重复)进行相同操作,并选择具有最大差值的A1BG结果。

3 个答案:

答案 0 :(得分:1)

您可以使用dplyr执行此操作:

new.df <- df %>% 
  rowwise %>% 
  mutate(difference = (mean(c(Control1, Control2, Control3)) - mean(c(Disease1, Disease2))))

...为您提供列中的行方差:

#    GENES    Control1   Control2    Control3   Disease1   Disease2   Difference
#   <fctr>       <dbl>      <dbl>       <dbl>      <dbl>      <dbl>      <dbl>
# 1    7A5  2.14718400 -0.3709474 -1.13216670  0.6824169  1.1014914 -0.6772642
# 2   A1BG -0.90922270  1.4587290 -1.30510790  0.1645328 -1.4461279  0.3889303
# 3   A1BG -1.48751000  1.4545832 -0.96583580  2.6276603  1.0965057 -2.1950039
# 4   A1BG -2.79559765 -0.2737989 -0.05914144  1.1173955 -1.5837953 -0.8096461
# 5   AAAS  0.05143231 -0.4511648 -0.20133056 -1.1359200 -0.1245733  0.4298923
# 6   AAAS -1.25764808  1.5628671  0.03029207 -0.1266691  0.5923233 -0.1209901
# 7   AFDS  0.61049620 -0.9225275  1.00159070 -0.4667365  0.2319656  0.3472386
# 8   AFDS  0.09226673 -0.5477966  1.18145151 -0.8015310  0.4698137  0.4078325
# 9   AFDS -0.80373550 -1.0586287  0.53609560 -1.1085319 -0.4540254  0.3391891

您可以使用这些值来选择每个“GENES”组中的最大行,方法是取消分组,按“GENES”重新分组,并使用最大值“差异值:

切割行。
new.df %>% 
  ungroup() %>% 
  group_by(GENES) %>% 
  slice(which.max(Difference))

#    GENES    Control1   Control2   Control3   Disease1   Disease2   Difference
#   <fctr>       <dbl>      <dbl>      <dbl>      <dbl>      <dbl>      <dbl>
# 1    7A5  2.14718400 -0.3709474 -1.1321667  0.6824169  1.1014914 -0.6772642
# 2   A1BG -0.90922270  1.4587290 -1.3051079  0.1645328 -1.4461279  0.3889303
# 3   AAAS  0.05143231 -0.4511648 -0.2013306 -1.1359200 -0.1245733  0.4298923
# 4   AFDS  0.09226673 -0.5477966  1.1814515 -0.8015310  0.4698137  0.4078325

这是使用data.table的另一种更短的方法:

setDT(df)[, 
  Difference := (mean(c(Control1, Control2, Control3)) - mean(c(Disease1, Disease2))), 
  by=1:nrow(df)
][, .SD[which.max(Difference)], .(GENES)]

#    GENES    Control1   Control2   Control3   Disease1   Disease2 Difference
# 1:   7A5  2.14718400 -0.3709474 -1.1321667  0.6824169  1.1014914 -0.6772642
# 2:  A1BG -0.90922270  1.4587290 -1.3051079  0.1645328 -1.4461279  0.3889303
# 3:  AAAS  0.05143231 -0.4511648 -0.2013306 -1.1359200 -0.1245733  0.4298923
# 4:  AFDS  0.09226673 -0.5477966  1.1814515 -0.8015310  0.4698137  0.4078325

如果您还不熟悉数据包,data.table解决方案可能会更难阅读,但第一组括号[ ]中的表达式计算平均值的行方差,然后是第二组括号内的表达式表示我们希望每组中的行具有最大差异(.SD[which.max(Difference)),然后按“GENES”分组。

答案 1 :(得分:1)

一种可能的解决方案是:

library(dplyr)

df %>%
  rowwise() %>%
  mutate(meanDiff = mean(c(Control1, Control2, Control3)) - mean(c(Disease1, Disease1)) ) %>%
  group_by(GENES) %>%
  filter(meanDiff == max(meanDiff)) %>%
  ungroup()

#Result:
# A tibble: 4 x 7
  GENES  Control1 Control2 Control3 Disease1 Disease2 meanDiff
  <fctr>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
1 7A5      2.15     -0.371   -1.13     0.682    1.10    -0.468
2 A1BG    -0.909     1.46    -1.31     0.165   -1.45    -0.416
3 AAAS     0.0514   -0.451   -0.201   -1.14    -0.125    0.936
4 AFDS     0.0923   -0.548    1.18    -0.802    0.470    1.04

答案 2 :(得分:1)

您可以使用基数R:

{{1}}