Hi, I have a vector with elements rs.id=c(''rs100'',''rs101'',''rs102'',''rs103'') And a dataframe ''snp.id'' 1 SNP_100 rs100 2 SNP_101 rs101 3 SNP_102 rs102 4 SNP_103 rs103 Task is to replace rs.id vector with corresponding ''SNP_'' ids in snp.id. Thanks in advance. Praveen Surendran 2G, Complex and Adaptive Systems Laboratory (UCD CASL) School of Medicine and Medical Sciences University College Dublin Belfield, Dublin 4 Ireland. [[alternative HTML version deleted]]
Hi, On Oct 22, 2009, at 10:12 AM, Praveen Surendran wrote:> Hi, > > I have a vector with elements > > rs.id=c('rs100','rs101','rs102','rs103') > > And a dataframe 'snp.id' > > 1 SNP_100 rs100 > 2 SNP_101 rs101 > 3 SNP_102 rs102 > 4 SNP_103 rs103 > > Task is to replace rs.id vector with corresponding 'SNP_' ids in > snp.id.First, please post your code and data in such a way that it's easy for those of us trying to help you to paste into our R session quickly, for instance, a better way to have given your data.frame would have been like so: R> > rs.id=c('rs100','rs101','rs102','rs103') R > df <- data.frame(snp.id=paste("SNP", 100:103, sep="_"), rs.id=rs.id, ) Now, assuming rs.id <-> snp.id is a one-to-one mapping: 1. One way to get the appropriate df$snp.id for each value in your rs.id vector is to use ``match``: R> df$snp.id[match(rs.id, df$rs.id)] [1] "SNP_100" "SNP_101" "SNP_102" "SNP_103" 2. Alternatively, you can use the "rs*" values as the rownames of your data.frame, and use the rs.id value to select: R> rownames(df) <- df$rs.id R> df[rs.id, 'snp.id'] [1] "SNP_100" "SNP_101" "SNP_102" "SNP_103" -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Try this: merge(rs.id, snp.id, by.x = 1, by.y = 2)$V2 or: with(snp.id, V2[match(rs.id, V3)]) On Thu, Oct 22, 2009 at 12:12 PM, Praveen Surendran <praveen.surendran at ucd.ie> wrote:> Hi, > > > > I have a vector with elements > > > > rs.id=c('rs100','rs101','rs102','rs103') > > > > And a dataframe 'snp.id' > > > > 1 ? ? ? ? ? ? ?SNP_100 ? ? ? ? ? ? ?rs100 > > 2 ? ? ? ? ? ? ?SNP_101 ? ? ? ? ? ? ?rs101 > > 3 ? ? ? ? ? ? ?SNP_102 ? ? ? ? ? ? ?rs102 > > 4 ? ? ? ? ? ? ?SNP_103 ? ? ? ? ? ? ?rs103 > > > > Task is to replace rs.id vector with corresponding 'SNP_' ids ?in snp.id. > > > > Thanks in advance. > > > > Praveen Surendran > > 2G, Complex and Adaptive Systems Laboratory (UCD CASL) > > School of Medicine and Medical Sciences > > University College Dublin > > Belfield, Dublin 4 > > Ireland. > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O