DF
sampleName realConc exptname concentrate timepoints replicate day var
name1_0 3.877049e-05 0hr 55mM 0 b1 011311 1
name1_20 3.293085e-04 kcl 55mM 20 b1 011311 2
name1_40 3.999433e-05 kcl 55mM 40 b1 011311 3
name2_0 2.939995e-03 0hr 55mM 0 b1 011411 1
name2_20 1.212584e-02 kcl 55mM 20 b1 011411 2
name2_40 1.894434e-02 kcl 55mM 40 b1 011411 3
我想将每个realConc值除以realConc值,时间点为0,具有相等的日,复制和集中值
我正在尝试for循环,而不是太多运气,你能帮帮我吗?
for (i in 1:dim(df)[1]){
df$realConc <- df$realConc[i] / df[which(duplicated(paste(replicate,day))) & df$timepoint == 0,]$realConc[i]
}
我在想这样的事情,但显然不起作用
答案 0 :(得分:3)
plyr是你的朋友!
library(plyr)
ddply(df, .(day, replicate, concentrate),
transform, scaled=realConc/realConc[timepoints==0])
# sampleName realConc exptname concentrate timepoints replicate day var scaled
# 1 name1_0 3.877049e-05 0hr 55mM 0 b1 11311 1 1.000000
# 2 name1_20 3.293085e-04 kcl 55mM 20 b1 11311 2 8.493793
# 3 name1_40 3.999433e-05 kcl 55mM 40 b1 11311 3 1.031566
# 4 name2_0 2.939995e-03 0hr 55mM 0 b1 11411 1 1.000000
# 5 name2_20 1.212584e-02 kcl 55mM 20 b1 11411 2 4.124442
# 6 name2_40 1.894434e-02 kcl 55mM 40 b1 11411 3 6.443664
答案 1 :(得分:1)
您尚未指定输出的外观,但这是执行该计算的一种方法:
首先,读入您的数据(最好使用dput()
或提供一些代码来重新创建数据。
test = read.table(header=TRUE, text = "sampleName realConc exptname concentrate timepoints replicate day var
name1_0 3.877049e-05 0hr 55mM 0 b1 011311 1
name1_20 3.293085e-04 kcl 55mM 20 b1 011311 2
name1_40 3.999433e-05 kcl 55mM 40 b1 011311 3
name2_0 2.939995e-03 0hr 55mM 0 b1 011411 1
name2_20 1.212584e-02 kcl 55mM 20 b1 011411 2
name2_40 1.894434e-02 kcl 55mM 40 b1 011411 3")
然后,根据您需要的分组拆分数据。
temp = split(test, list(test$day, test$concentrate, test$replicate))
第三,按组计算realConc
的{{1}}值,然后用它来进行划分。
timepoints == 0
lapply(temp, function(x) x[, 2]/x[which(x$timepoints == 0), 2])
# $`11311.55mM.b1`
# [1] 1.000000 8.493793 1.031566
#
# $`11411.55mM.b1`
# [1] 1.000000 4.124442 6.443664
答案 2 :(得分:0)
这是一个简单的基础R版本。调试可能更容易,因为每个步骤都是可见的。
# find all possible denominators and rename realConc to avoid duplicate name in merge
denom <- x[x$timepoints == 0,c('realConc','concentrate','replicate','day')]
names(denom)[1] <- 'realConcDenominator'
# merge in new column with appropriate denominator
x$realConcDenominator <- merge(x,denom,by = c('concentrate','replicate','day'),all.x = T)[,'realConcDenominator']
# and divide
x$result <- x$realConc / x$realConcDenominator
另一个使用申请。
# or use apply in one shot
x$applyresult <- apply(x,1,function(x,denom){
as.numeric(x['realConc'])/denom[denom$concentrate == x['concentrate'] & denom$replicate == x['replicate'] & denom$day == x['day'],'realConc']
},denom = x[x$timepoints == 0,c('realConc','concentrate','replicate','day')])