对于n行,减去一个数字;然后下一组n行切换到一个新数字,从

时间:2015-04-25 23:30:51

标签: r

我在一个大型数据框中有一系列读数,如下所示:

Plate_ID    Day Well_ID Name    X590_Mean   X590_SD X750_Mean   X750_SD
1   MCBA15 001  0   SPL1    Water   0.196   0.003   0.145   0.004
2   MCBA15 001  0   SPL2    Pyruvic Acid Methyl Ester   0.202   0.001   0.143   0.000
3   MCBA15 001  0   SPL3    Tween 40    0.214   0.036   0.158   0.026
4   MCBA15 001  0   SPL4    Tween 80    0.196   0.000   0.144   0.002
5   MCBA15 001  0   SPL5    ?-Cyclodextrin  0.217   0.012   0.161   0.012
...
33  MCBA15 001  1   SPL1    Water   0.209   0.008   0.111   0.003
34  MCBA15 001  1   SPL2    Pyruvic Acid Methyl Ester   0.371   0.007   0.148   0.003
35  MCBA15 001  1   SPL3    Tween 40    0.481   0.127   0.285   0.088
36  MCBA15 001  1   SPL4    Tween 80    0.242   0.011   0.108   0.002
37  MCBA15 001  1   SPL5    ?-Cyclodextrin  0.277   0.002   0.138   0.001

基本上,我需要做的是减去每个590 and 750Water的{​​{1}}行中的平均值(Plate)仅适用于那些行共享那些相同的变量(即每32行,切换到下一个平均值以从后续行中减去)

所需的输出应为:

Day

Plate_ID Day Well_ID Name X590_Mean X590_SD X750_Mean X750_SD 1 MCBA15 001 0 SPL1 Water 0.000 0.003 0.000 0.004 2 MCBA15 001 0 SPL2 Pyruvic Acid Methyl Ester 0.006 0.001 0.000 0.000 3 MCBA15 001 0 SPL3 Tween 40 0.018 0.036 0.013 0.026 ... 33 MCBA15 001 1 SPL1 Water 0.000 0.008 0.000 0.003 34 MCBA15 001 1 SPL2 Pyruvic Acid Methyl Ester 0.162 0.007 0.037 0.003 成为negative的任何值都应由0

组成

我尝试了以下方法,但遇到了很多困难。

sp2 <- split(dat, with(dat, interaction(Plate_ID, Day)))
sapply(sp2, dim)
d <- function(biolog) {
  x <- biolog$X590_Mean[1]
  biolog$X590_Mean[biolog$X590_Mean > x] <- biolog$X590_Mean - x
  y <- biolog$X750_Mean[1]
  biolog$X750_Mean[biolog$X750_Mean > y] <- biolog$X750_Mean - y
}
lapply(sp2, d)

1 个答案:

答案 0 :(得分:1)

您可以尝试此解决方案:

#Start of my Code#
test1<-function(c,x){
high=0
low=0
samp=NULL

samp=sample(c,x)
for(i in 1:x){
if(samp[i]>1){high=high+1}
else if (samp[i]<0){low=low+1}}
c(high,low,mean(samp),var(samp),samp)
}

sim1 <-function(c,x){
replicate(nsim,{test1(c,x)})}

size=10
a<-sim1(overall,size)

listnormwor=NULL
countnormwor=0
meannormwor=NULL

for(i in 0:nsim-1){
**if (a[1+(size+4)*i]+a[2+(size+4)*i]==0)**{ 
countnormwor=countnormwor +1
for (z in 5:(size+4)){ 
listnormwor=c(listnormwor, a[z+(size+4)*i])}
meannormwor=c(meannormwor,a[3+(size+4)*i])}
}

countnormwor
mean(meannormwor)
var(listnormwor)

(我没有在实际数据集上测试过。如果此解决方案有任何问题,请提供d <- function(biolog) { biolog$X590_Mean <- with(biolog, X590_Mean - X590_Mean[1]) biolog$X590_Mean <- ifelse(biolog$X590_Mean < 0, 0, biolog$X590_Mean) biolog$X750_Mean <- with(biolog, X750_Mean - X750_Mean[1]) biolog$X750_Mean <- ifelse(biolog$X750_Mean < 0, 0, biolog$X750_Mean) biolog }