[R] fetching columns from another file

William Dunlap wdunlap at tibco.com
Sun Jan 24 20:01:00 CET 2010


Have you tried using merge?  E.g., something
like
   PCC <- merge(ptable[c("Gene1", "Gene2"),], table, suffices=c("",""))

By the way, why do you convert the output of
read.table to a matrix?  Since you have both
character and numeric data columns I think it
would make more sense to leave the dataset as
a data.frame (which read.table produces).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Amit
> Sent: Sunday, January 24, 2010 10:48 AM
> To: r-help at r-project.org
> Subject: [R] fetching columns from another file
> 
> Hi! All,
> 
> I am trying to fetch rows from a data frame which matches to first 2
> columns of another data frame. Here is the example what I am trying to
> do:
> > ptable=read.table(file="All.txt",header=T,sep="\t")
> > ptable=as.matrix(ptable)
> > dim(ptable)
> [1] 9275    6
> > head(ptable)
>      Gene1       Gene2       PCC         PCC3        PCC23    
>    PCC123
> [1,] "3813_f_at" "3884_f_at" "0.9956842" "0.9955455" 
> "0.9956513" "0.9956171"
> [2,] "3884_f_at" "3813_f_at" "0.9956842" "0.9955455" 
> "0.9956513" "0.9956171"
> [3,] "3491_f_at" "3709_f_at" "0.9952116" "0.9951588" 
> "0.9951601" "0.9950864"
> [4,] "3709_f_at" "3491_f_at" "0.9952116" "0.9951588" 
> "0.9951601" "0.9950864"
> [5,] "3371_f_at" "3594_f_at" "0.9946206" "0.9945342" 
> "0.9946246" "0.9946592"
> [6,] "3594_f_at" "3371_f_at" "0.9946206" "0.9945342" 
> "0.9946246" "0.9946592"
> > table=read.table(file="All_GPYeast_m.txt",header=T,sep="\t")
> > table=as.matrix(table)
> > dim(table)
> [1] 9275    6
> > head(table)
>      Gene1       Gene2       PCC         PCC3        PCC23    
>    PCC123
> [1,] "3491_f_at" "3709_f_at" "0.9953142" "0.9950756" 
> "0.9954676" "0.9952902"
> [2,] "3709_f_at" "3491_f_at" "0.9953142" "0.9950756" 
> "0.9954676" "0.9952902"
> [3,] "3813_f_at" "3884_f_at" "0.9951781" "0.9953901" 
> "0.9959256" "0.9958152"
> [4,] "3884_f_at" "3813_f_at" "0.9951781" "0.9953901" 
> "0.9959256" "0.9958152"
> [5,] "3371_f_at" "3594_f_at" "0.9946130" "0.9938905" 
> "0.9945572" "0.9945285"
> [6,] "3594_f_at" "3371_f_at" "0.9946130" "0.9938905" 
> "0.9945572" "0.9945285"
> 
> Now, I wish to pick column 1&2 from 'ptable' and their coresponding
> columns from 'table' and store it in a variable. I did following and
> got error
> > PCC=apply(ptable[,c(1,2)],1,function(x)table[x[1],x[2]])
> Error in FUN(newX[, i], ...) : subscript out of bounds
> 
> I was expecting something like this
> >head(PCC)
> [1,] "3813_f_at" "3884_f_at" "0.9951781" "0.9953901" 
> "0.9959256" "0.9958152"
> [2,] "3884_f_at" "3813_f_at" "0.9951781" "0.9953901" 
> "0.9959256" "0.9958152"
> ---------------------
> ---------------------
> 
> Please, help!
> 
> regards
> Amit
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list