我想使用字符串向量中的变量名称在R中调用function()
。在我的特定情况下,我使用了for循环来生成编号的变量名称。例如:
for (i in 1:3) {
assign(paste0("x",i),"value")
}
v <- paste0("x",c(1,2,3))
function(v) #Here I would like to call function with variables defined in for loop.
背景:
我有一个数据框,可以通过不同的条件(控件,案例)进行过滤。生成的向量将变成Grange对象,并保存为变量或另存为目录的字符串向量。然后,我想使用相同数量的Grange对象和路径调用IDRfilterN3()
。该函数需要3个Grange对象和3个路径。
下面是具体示例:
for (i in unique(samples$Treatment)){
ind <- samples$Treatment == i
for (j in samples$Replicate[ind]){
assign(paste0("peaks_",i,"_",j),toGRanges(file.path(samples$Peaks[ind][j]),format = "MACS2"))
assign(paste0("bam_",i,"_",j),file.path(samples$bamReads[ind][j]))
}
if(length(samples$Replicate[ind])==3) assign(paste0("peaks_",i,".idr"),IDRfilterN3(paste0("peaks_",i,"_",c(1,2,3)),paste0("bam_",i,"_",c(1,2,3))))
else print("Sample size not applicable. IDR failed!")
}
这是示例工作表的示例和我需要执行的步骤:
ids <- paste0("ID",c(1,2,3,4))
condition <- c(rep("A",2),rep("B",2))
replicate <- c(1,2,1,2)
pathA <- paste0("/dir/sample_",c(1,2,3,4))
pathB <- paste0("/dir/sample_",c(1,2,3,4),".bam")
d <- data.frame(ids,condition,replicate,pathA,pathB)
#Generate variable storing Grange object out of pathA path for each condition and replicate:
peakA1 <- toGRanges("/dir/sample_1")
peakA2 <- toGRanges("/dir/sample_2")
peakB1 <- toGRanges("/dir/sample_3")
peakB2 <- toGRanges("/dir/sample_4")
#Call IDR function for each condition (A, B) using the replicate variables and the path from column pathB
IDRfilterN3(peakA1, peakA2, "/dir/sample_1.bam", "/dir/sample_2.bam")
IDRfilterN3(peakB1, peakB2, "/dir/sample_3.bam", "/dir/sample_4.bam")
感谢您的输入。