R - 重复多个"锚点的功能"

时间:2016-07-19 20:51:10

标签: r statistics repeat

很抱歉,如果这是一个措辞不好的问题,我对在R中组织数据的最佳方式感到困惑。这是数据框:

 Experiment Treatment              Inhibitor      pAKT Loading Control
          1      None                   DMSO  7075.426       16051.376
          1 Carbachol                   DMSO 18368.962       14826.962
          1 Carbachol          10-8 M BYL179 14949.184       10911.477
          1 Carbachol          10-7 M BYL179 11972.477       10993.305
          1 Carbachol          10-6 M BYL179 14874.841       12610.841
          1 Carbachol          10-8 M Cal101 11433.598       12305.891
          1 Carbachol          10-7 M Cal101 10297.719       13175.012
          1 Carbachol          10-6 M Cal101  2835.891       11609.598
          1 Carbachol 10-8 M BYL179 + Cal101  8749.134       12024.012
          1 Carbachol 10-7 M BYL179 + Cal101  4490.841       13088.598
          1 Carbachol 10-6 M BYL179 + Cal101   598.012       10929.134
          2      None                   DMSO 11904.225       30755.104
          2 Carbachol                   DMSO 27637.518       27536.912
          2 Carbachol          10-8 M BYL179 13970.619       24052.326
          2 Carbachol          10-7 M BYL179  7567.326       21889.154
          2 Carbachol          10-6 M BYL179  8421.326       13446.033
          2 Carbachol          10-8 M Cal101  4754.497        7333.912
          2 Carbachol          10-7 M Cal101  3614.497        3566.376
          2 Carbachol          10-6 M Cal101  1426.598        1331.326
          2 Carbachol 10-8 M BYL179 + Cal101  7401.175        6010.447
          2 Carbachol 10-7 M BYL179 + Cal101  3159.740        5280.154
          2 Carbachol 10-6 M BYL179 + Cal101  2114.376        9793.347
          3      None                   DMSO  4528.690       20969.347
          3 Carbachol                   DMSO 27218.104       23433.912
          3 Carbachol          10-8 M BYL179 12507.619       21712.033
          3 Carbachol          10-7 M BYL179  4673.790       13934.154
          3 Carbachol          10-6 M BYL179  6699.033        8018.497
          3 Carbachol          10-8 M Cal101 10108.861       17364.861
          3 Carbachol          10-7 M Cal101 10728.326       25307.447
          3 Carbachol          10-6 M Cal101  7418.083       20826.012
          3 Carbachol 10-8 M BYL179 + Cal101 17169.104       23850.255
          3 Carbachol 10-7 M BYL179 + Cal101 14890.811       36585.368
          3 Carbachol 10-6 M BYL179 + Cal101  5889.497       19609.033
          4      None                   DMSO  5029.841       14217.083
          4       EGF                   DMSO 17963.719       16883.255
          4       EGF          10-8 M BYL179 18191.719       15706.719
          4       EGF          10-7 M BYL179 17430.719       16327.891
          4       EGF          10-6 M BYL179 15350.305       14828.134
          4       EGF          10-8 M Cal101 15847.891       14122.598
          4       EGF          10-7 M Cal101 17678.426       16659.719
          4       EGF          10-6 M Cal101 14236.477       15380.012
          4       EGF 10-8 M BYL179 + Cal101 14281.891       15367.012
          4       EGF 10-7 M BYL179 + Cal101 13918.012       13796.062
          4       EGF 10-6 M BYL179 + Cal101  4702.062       12578.012
          5      None                   DMSO 14192.004       40446.196
          5       EGF                   DMSO 43425.246       46626.125
          5       EGF          10-8 M BYL179 43465.004       45206.832
          5       EGF          10-7 M BYL179 34098.882       36853.175
          5       EGF          10-6 M BYL179 33415.882       37637.539
          5       EGF          10-8 M Cal101 32413.175       31047.054
          5       EGF          10-7 M Cal101 32359.882       35967.004
          5       EGF          10-6 M Cal101 27089.761       33929.125
          5       EGF 10-8 M BYL179 + Cal101 30945.882       29804.882
          5       EGF 10-7 M BYL179 + Cal101 32511.711       37228.832
          5       EGF 10-6 M BYL179 + Cal101 10176.832       36218.024
          6      None                   DMSO 17417.004       47193.439
          6       EGF                   DMSO 49204.075       55932.489
          6       EGF          10-8 M BYL179 45682.832       52486.125
          6       EGF          10-7 M BYL179 37131.711       41015.004
          6       EGF          10-6 M BYL179 32859.761       33375.933
          6       EGF          10-8 M Cal101 39127.711       33037.004
          6       EGF          10-7 M Cal101 35535.589       33514.539
          6       EGF          10-6 M Cal101 25455.175       28547.882
          6       EGF 10-8 M BYL179 + Cal101 21374.589       24441.296
          6       EGF 10-7 M BYL179 + Cal101 19590.125       22268.660
          6       EGF 10-6 M BYL179 + Cal101  5347.782       21943.317

我想创建一个标题为" Correction Factor"的新列,通过将具有条目的加载控制值除以条目与处理"无"和抑制剂" DMSO"对于那个实验。例如,实验1的第一个条目是16051.376 / 16051.376 = 1,第二个条目是14826.962 / 16051.376 = 0.9237,第三个条目是10911.477 / 16051.376 = 0.6798,依此类推。对于实验2,我将除以实验2无+ DMSO条目,30755.104。如何为每个实验重复此操作?

1 个答案:

答案 0 :(得分:2)

虽然这肯定可以在基础R中完成,但Hadl​​ey Wickham的工具提供了一种很好的方法(按一些标准拆分数据框,对子集应用转换,并将它们重新组合在一起 - &# 34; split-apply-combine"简称)。

(这些未经测试但我认为它们应该有用......)

library(plyr)
library(dplyr) ## load dplyr *second!*

plyr(Hadleyverse / tidyverse 1)

ddply(my_data,"Experiment",
     mutate,
         Correction.Factor=Loading.Control/
           Loading.Control[Treatment=="None" & Inhibitor=="DMSO"])

dplyr(Hadleyverse / tidyverse 2)

my_data %>% group_by(Experiment) %>%
     mutate(Correction.Factor=Loading.Control/
           Loading.Control[Treatment=="None" & Inhibitor=="DMSO"])

另一种(base-R-friendly)方法是对基线(无/ DMSO)情况进行子集化,仅保留Experiment和Loading.Control列(但重命名后者),然后merge结果返回原始数据集 - 然后你要做的就是将一列与另一列分开......