[R] How to convert a vector into a list

Liaw, Andy andy_liaw at merck.com
Thu Aug 19 18:39:30 CEST 2004


1.  When you don't know (or are not sure) what an object is, str() is your
friend.

2.  My guess is that `lidnames' is a character vector with names.

3.  If genes.txt has only only column, you might as well use:

     mygenes <- scan("genes.txt", what="")

    which reads the data into mygenes as a character vector.  Then your
command should work.

[read.table() returns a data frame.  You index mygenes, a data frame, with a
logical vector, which means selecting columns that are TRUE.  I suspect
mygenes, as you have it, is a data frame of only one column, so the
subsetting won't work.]

HTH,
Andy

> From: S Peri
> 
> Dear group, 
> Apologies for asking the most chomped FAQ.
> 
> I have a file with list of gene names(genes.txt):
> EGF
> EGFR
> PTPN6
> TIEG2
> MAPK1
> 
> 
> I have another object in R, I do not know the data
> type for that object that looks like this
> ("lidnames"):
> "RABGGTA"   "MAPK3"     "TIE" "CYP2C19" 
> 
> 
> > lidnames[1:10]
>  100_g_at   1000_at   1001_at 1002_f_at 1003_s_at    
> "RABGGTA"   "MAPK3"     "TIE" "CYP2C19"    "BLR1" 
> 
> 
> I want to pick list of genes from lidnames object that
> are in genes.txt.  I am using %in% function.
> 
> >mygenes<-read.table("genes.txt")
> 
> > mygenes[mygenes %in% lidnames]
> NULL data frame with 164 rows
> > 
> 
> I am unable to pullout genes from lidnames object.
> 
> Is it because that lidnames is as a list type and
> mygenes object is as a vector/matrix type.
> 
> How can I convert mygenes to list type where I can
> have the elements:
> 
> "EGF","EGFR","PTPN6","TIEG2","MAPK1".
> 
> I think in this way I can pull out the names from
> lidnames object or any other complex matrix. 
> 
> Please help me. 
> 
> Thank you
> SP
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list