[R] replacing values in a vector

Iain Gallagher iaingallagher at btopenworld.com
Thu Nov 6 23:46:14 CET 2008


Hello list.

I have a vector of values:

eg

> head(diff_mirs_list)
[1] "hsa-miR-26b" "hsa-miR-26b" "hsa-miR-23a" "hsa-miR-27b" "hsa-miR-29a"
[6] "hsa-miR-29b"

and I would like to conditionally replace each value in this vector with a number defined in a dataframe:

> fc
             Probe ave.fc
1       hsa-let-7a   1.28
2      hsa-miR-100   1.47
3  hsa-miR-125a-5p   1.31
4   hsa-miR-140-3p   1.28
5      hsa-miR-143   1.98
6  hsa-miR-193a-3p   1.37
7     hsa-miR-193b   1.48
8      hsa-miR-195   1.16
9      hsa-miR-214   1.22
10     hsa-miR-23a   1.21
11     hsa-miR-26b   1.13
12     hsa-miR-27b   1.37
13     hsa-miR-29a   1.24
14     hsa-miR-29b   1.69
15     hsa-miR-30b   1.16
16     hsa-miR-424   1.42
17  hsa-miR-768-3p   1.48
18  hsa-miR-886-3p   1.43
19     hsa-miR-933   1.23

ie every hsa-let-7a in the diff_mirs_list is replaced by 1.28, hsa-miR-100 by 1.47 etc etc

I have tried to make a loop to use gsub eg

> for (i in 1:nrow(fc)){
+ test<-gsub(fc[i,1], fc[i,2], diff_mirs_list)
}

but this obviously passes the unchanged vector to gsub each time and so I get back my 'test' vector with only hsa-miR-933 changed. Could someone help me out with this please. 

Thanks

Iain

> sessionInfo()
R version 2.8.0 (2008-10-20) 
i486-pc-linux-gnu 

locale:
LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base  



More information about the R-help mailing list