[R] How to use a value of an aboject in a line command?

Petr Savicky savicky at praha1.ff.cuni.cz
Sun Feb 6 21:11:34 CET 2011


On Sun, Feb 06, 2011 at 04:31:43PM -0200, Leandro Colli wrote:
> This is my first time here at R Forum!
> 
>     I am a new user of R. I am very happy with this fabulous software!
> 
>     I know how to use greps, fors, seeds, %in%, paste, etc.
> 
>     But I need to know how to use  a value of an aboject in a line command?
> 
>     Example:
> > i
> [1] "TP53"
> 
> > TP53
>   V1   V2  V3
> 1  1 TP53 1.1
> 2  2 TP53 1.2
> 3  3 TP53 1.3
> 
> I would like to do a t.test of TP53 x control.
> But this is a gene list.
> My line command would be
> 
> result = t.test( i[,3], control[,3]).
> 
> But using this, the test will be in the object i and I would like to do in
> the object which is value of i.
> 
> I though to use `print(i)`, like Shell, to modify my test line command, but
> i does not works.
> 
> Do I was clear? I need to do it because the test will be done in a list of
> genes (for i in genes).

If i understand correctly, then what you ask for is

  result = t.test( get(i)[,3], control[,3])

However, get() is only rarely needed. If you have a list of tables,
you can have them all in a single list variable and use a loop
over the list. I mean something like the following

  tab <- list()
  tab[[1]] <- data.frame(V1=1:3, V2="AA", V3=seq(1.1, 1.3, by=0.1))
  tab[[2]] <- data.frame(V1=1:3, V2="BB", V3=seq(2.1, 2.3, by=0.1))
  tab[[3]] <- data.frame(V1=1:3, V2="CC", V3=seq(3.1, 3.3, by=0.1))
  
  for (j in 1:3) {
      # here any command using tab[[j]] may be used
      # using print() for simplicity
      print(tab[[j]])
  }

See chapter 6 Lists and data frames of R-intro.pdf available at

  http://cran.r-project.org/manuals.html

Petr Savicky.



More information about the R-help mailing list