[R] Replacing multiple elements in a vector !
Steve Lianoglou
mailinglist.honeypot at gmail.com
Thu Oct 22 17:33:30 CEST 2009
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
More information about the R-help
mailing list