如何在for循环中创建一个带有函数的对象?每个新对象名称都是NAME [后缀]

时间:2018-02-21 22:12:20

标签: r

我正在运行一个for循环,为每次迭代创建对象。该功能称为量具。您将从下面的脚本中注意到我想要为PB8创建BP1,BP2,BPi,并为MF和CC创建相同的内容。 但是如果我跑:

paste("BP",i,sep="") = gage(CNTS, gsets=go.bp, ref= go.ref.idx, compare = "as.group", samp = go.samp.idx)

我收到了这个错误:

  

粘贴错误(“BP”,i,sep =“”)= gage(CNTS,gsets = go.bp,ref =   go.ref.idx,:赋值目标扩展为非语言对象

我怎样才能做我想做的事?

到目前为止,我的脚本是

```{r}
all_idx = 1:32
go.ref.idx=c()
go.samp.idx=c()
for(i in seq(from=0, to=7, by=1)){
        go.ref.idx= c((1+i*4):(4+i*4))
        go.samp.idx = setdiff(all_idx, c( (1+i*4):(4+i*4)))

        BP = gage(CNTS, gsets=go.bp, ref= go.ref.idx, compare = "as.group", samp = go.samp.idx)
        MF = gage(CNTS, gsets=go.mf, ref= go.ref.idx, compare = "as.group", samp = go.samp.idx)
        CC = gage(CNTS, gsets=go.cc, ref= go.ref.idx, compare = "as.group", samp = go.samp.idx)
}
```

但只运行las索引的值。 我将非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

感谢您的建议:以下代码解决了我的问题:

```{r}

all_idx = 1:32
go.ref.idx=c()
go.samp.idx=c()
GOperCluster_Vehicle = list()
for(i in seq(from=0, to=7, by=1)){
        go.samp.idx= c((1+i*4):(4+i*4))
        go.ref.idx = setdiff(all_idx, c( (1+i*4):(4+i*4)))

        BP = gage(CNTS, gsets=go.bp, ref= go.ref.idx, compare = "as.group", samp = go.samp.idx)
        MF = gage(CNTS, gsets=go.mf, ref= go.ref.idx, compare = "as.group", samp = go.samp.idx)
        CC = gage(CNTS, gsets=go.cc, ref= go.ref.idx, compare = "as.group", samp = go.samp.idx)
        name = paste("Cluster", i , sep="")
        tmp = list(BP= BP, MF=MF, CC=CC)
        GOperCluster_Vehicle[[name]] = tmp
}
```