Use paste in for loop R
1
0
Entering edit mode
7.7 years ago
Benn 8.3k

Hi Guys,

I have always some trouble when I have to translate my BASH strategy to R. I am using edgeR for differential analysis, and have 16 lrts, named lrt1 to lrt16. Now I want to make MA plot in a for loop, and want to use paste to direct to the lrt1 to lrt16, like this.

i=1
summaryDE<-summary(de <- decideTestsDGE(paste0("lrt",i), p=0.05))
Error in decideTestsDGE(paste0("lrt", i), p = 0.05) : 
  Need DGEExact or DGELRT object

When I use lrt1 instead of the paste0 stuff it does work :/

What did I forget? Thanks in advance!

Ben

edgeR R • 21k views
ADD COMMENT
0
Entering edit mode

My first guess is that your paste returns a string, not the object. A real simple toy example shows you that:

a22 = "foo"
i = 22
cat(paste0("a", i))

This cat returns the string "a22" and not the value of the variable a22 ('foo')

In contrast, observe the following:

a1 = "foo"
a2 = "bar"
a3 = "baz"
for (i in c(a1,a2,a3)) { cat(i) }

This cat returns 'foobarbaz' (no separator specified) and accesses the value behind the object.

ADD REPLY
0
Entering edit mode

Thanks Wouter, if I understand correct it is not possible to paste the object names together in a for loop? But I have to first make a list with lrt1 to lrt16, and loop thru it?

ADD REPLY
0
Entering edit mode

That would work I guess. How did you create the lrt objects? Perhaps you can just append each object, after it's created, to a vector and then loop over it. Or if you are working interactively, do some lazy stuff like paste(paste0("lrt", 1:16), collapse = ', ') and copy paste that in your new vector

Looks stupid, might be a better way to do this but if it works it's no longer stupid right?

ADD REPLY
1
Entering edit mode

I found out that I have to put get before it...

> summaryDE<-summary(de <- decideTestsDGE(get(paste0("lrt",i)), p=0.05))

Thanks for the help!

ADD REPLY
1
Entering edit mode

So there was a better way to do it! Good job ;-)

ADD REPLY
4
Entering edit mode
7.7 years ago
Guangchuang Yu ★ 2.6k
Error in decideTestsDGE(paste0("lrt", i), p = 0.05) : 
  Need DGEExact or DGELRT object

this is obvious, paste0("lrt", i) will work and outptu "lrt1" and use it as input of decideTestsDGE which require DGEExact or DGELRT object. But here the input "lrt1" is just a character object.

see the function definition in https://github.com/Bioconductor-mirror/edgeR/blob/master/R/decidetestsDGE.R

Line 8:

 if(!is(object,"DGEExact") & !is(object,"DGELRT")) stop("Need DGEExact or DGELRT object") # Expects a DGEExact or DGELRT object

throw you the error.

This is not an issue of paste0, but the input required by decideTestsDGE.


What you want is to input lrt1 object. In your code, you are inputting "lrt1" character string, but not the object you are trying to refer.

You can use eval(parse(text=paste0("lrt", i))) to parse "lrt1" to get the corresponding object.

So you code should be:

summaryDE<-summary(de <- decideTestsDGE(eval(parse(text=paste0("lrt", i))), p=0.05))
ADD COMMENT
0
Entering edit mode

I wasn't aware of this eval magic :-D nice!

ADD REPLY
0
Entering edit mode

Thanks for your help!

ADD REPLY

Login before adding your answer.

Traffic: 2640 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6