Question: (Closed) loop several conditions R
0
gravatar for julrodr80
6 months ago by
julrodr800
Spain
julrodr800 wrote:

I need to do a loop with several conditions (create three diferent objects several times) and with a loop nested. I write this:

`metaxcan <- c("foo1.csv", "foo2.csv")`
`predix_asso <- c("soo1.csv", "soo2.csv")`

`for (i in metaxcan){`
`for (j in predix_asso){`
  `PGC<-read.csv(i, header=T, sep=",")`
  `asociacion<-read.table(j, header=T, sep="") `

`PGC_predix <- merge(PGC,asociacion,by="gene")`
   `ngenes<-nrow(PGC_predix_1)  ` 



 `print(ngenes)` 

  `}` 
 `}`

But instead of print just 2 numbers (the merge between foo1 and soo1; and the merge of foo2 and soo2), it prints 4 numbers (all interactions)

How can I do? thanks

loop R • 293 views
ADD COMMENTlink modified 5 months ago by manuel.belmadani920 • written 6 months ago by julrodr800

Not sure if that question really fits here, but for now your code is faulty:

ngenes<-nrow(PGC_predix_1)

PGC_predix_1 is undefined, or possibly defined elsewhere in your environment, maybe PGC_predix was meant. That is why you get the same output, the number of rows in this variable. It prints 4 times, because i and j each have two elements and 2x2=4.

ADD REPLYlink modified 6 months ago • written 6 months ago by Michael Dondrup46k

If your file name vectors always have the same length then there is no need of nested loop, the single loop should work.

for example:

metaxcan <- c("foo1.csv", "foo2.csv")
predix_asso <- c("soo1.csv", "soo2.csv")

for (i in c(1:length(metaxcan)) {
    PGC<-read.csv(metaxcan[i], header = T, sep = ",")
    asociacion<-read.table(predix_asso[i], header = T, sep = ",")     # sep = ",", if it is same as metaxcan tab seperated file
    PGC_predix <- merge(PGC,asociacion,by = "gene")
    print (nrow(PGC_predix))   # PGC_predix_1 is an undefined variable. used in you quesion
}
ADD REPLYlink modified 5 months ago • written 5 months ago by Nitin Narwade400

Hello julrodr80!

We believe that this post does not fit the main topic of this site.

Pure R question, please search StackOverflow

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

ADD REPLYlink written 5 months ago by RamRS22k
0
gravatar for manuel.belmadani
5 months ago by
Canada
manuel.belmadani920 wrote:

But instead of print just 2 numbers (the merge between foo1 and soo1; and the merge of foo2 and soo2), it prints 4 numbers (all interactions)

4 numbers makes sense because you're merging: foo1 + soo1, foo1 + soo2, foo2 + soo1, foo2 + soo2 and printing the ngenes after each j loop.

Sounds like you want to do them pairwise (foo1 + soo1, foo2 + soo2). If that's the case, you could do something like:

for (index in length(metaxcan) ) { # Assuming they're the same size as predix_asso.
    i = metaxcan[index] # Obtain them pairwise
    j = predix_asso[index]

    PGC<-read.csv(i, header=T, sep=",")
    asociacion<-read.table(j, header=T, sep="") 

    PGC_predix <- merge(PGC,asociacion,by="gene")
    ngenes<-nrow(PGC_predix_1)   

    print(ngenes) 
}
ADD COMMENTlink written 5 months ago by manuel.belmadani920
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1664 users visited in the last hour