0

I am running a for loop that creates objects for each iteration. The function is called gage. You will notice from the script below that I wanted to create BP1, BP2, BPi to PB8 and the same for MF and CC. However if I run :

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

I got this error:

Error in paste("BP", i, sep = "") = gage(CNTS, gsets = go.bp, ref = go.ref.idx, : target of assignment expands to non-language object

How can I do what I want?

My script so far is

```{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)
}
```

But only runs the values of the las index. I will appreciate your help

neilfws
  • 32,751
  • 5
  • 50
  • 63
ALejandro
  • 45
  • 3
  • 2
    The answer is that your approach is wrong. In R we don't create collections of individual objects with similar names. We put those related objects into a single list. So you would create a list of the appropriate length, and all the "BP"-ish objects would go in that single list, one at a time. – joran Feb 21 '18 at 22:14

1 Answers1

0

thanks for the suggestion: the below code solve my problem:

```{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
}
```
ALejandro
  • 45
  • 3