您for
失败是因为您忘记使用get
功能
library(MALDIquant) # You should write what package is used
`Names <- c('a', 'b','c')` # this a vector of names
for(i in Names){
assign(i, createMassPeaks(mass=get(i)[, 1], intensity=get(i)[, 2]))
}
这个循环应该可以工作。让我们测试一下
set.seed(001)
a <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))
b <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))
c <- data.frame(V1=rnorm(50, 1000, 50), V2=rnorm(50, 1000, 50))
结果如下所示:
> a
S4 class type : MassPeaks
Number of m/z values : 50
Range of m/z values : 889.265 - 1079.764
Range of intensity values: 9.098e+02 - 1.12e+03
> b
S4 class type : MassPeaks
Number of m/z values : 50
Range of m/z values : 904.282 - 1104.358
Range of intensity values: 9.256e+02 - 1.115e+03
> c
S4 class type : MassPeaks
Number of m/z values : 50
Range of m/z values : 855.554 - 1124.883
Range of intensity values: 8.798e+02 - 1.132e+03
尽管这个for
循环运行良好,@David Robinson 的回答是最好的。我还想说一件事,我觉得你的函数应该是这样的:
as.MassPeaks <- function(x) {
return(createMassPeaks(mass=x[, 1],
intensity=x[, 2],
metaData=x))}
mass=x[, 1]
有效但mass=mass(x[, 1])
无效,对于instensity
and 也是如此metaData
。然后使用大卫罗宾逊的方法,你应该得到:
lapply(list(a,b,c), as.MassPeaks)
[[1]]
S4 class type : MassPeaks
Number of m/z values : 50
Range of m/z values : 889.265 - 1079.764
Range of intensity values: 9.098e+02 - 1.12e+03
[[2]]
S4 class type : MassPeaks
Number of m/z values : 50
Range of m/z values : 904.282 - 1104.358
Range of intensity values: 9.256e+02 - 1.115e+03
[[3]]
S4 class type : MassPeaks
Number of m/z values : 50
Range of m/z values : 855.554 - 1124.883
Range of intensity values: 8.798e+02 - 1.132e+03