[R] replacing values in one vector with corresponding index from another vector
A quick question for the gurus... Given: a=c(58,73,100,40,70) b=c(40,70,73,100,58,70,70,58) How can I replace the elements of b with the corresponding index numbers from a that start at 1? All values in a are unique. So, I end up with: b=c(4,5,2,3,1,5,5,1) I believe I need to use one of the apply functions in combination with which but I cannot make it work. thank you, -david __ R-help@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.
Re: [R] replacing values in one vector with corresponding index from another vector
How about a trick? b - as.numeric(factor(b, levels=a)) b [1] 4 5 2 3 1 5 5 1 On Wed, Oct 19, 2011 at 5:38 PM, David Epstein david...@umich.edu wrote: A quick question for the gurus... Given: a=c(58,73,100,40,70) b=c(40,70,73,100,58,70,70,58) How can I replace the elements of b with the corresponding index numbers from a that start at 1? All values in a are unique. So, I end up with: b=c(4,5,2,3,1,5,5,1) I believe I need to use one of the apply functions in combination with which but I cannot make it work. thank you, -david -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
Re: [R] replacing values in one vector with corresponding index from another vector
match(b, a) [1] 4 5 2 3 1 5 5 1 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Epstein Sent: Wednesday, October 19, 2011 2:39 PM To: r-help@r-project.org Subject: [R] replacing values in one vector with corresponding index from another vector A quick question for the gurus... Given: a=c(58,73,100,40,70) b=c(40,70,73,100,58,70,70,58) How can I replace the elements of b with the corresponding index numbers from a that start at 1? All values in a are unique. So, I end up with: b=c(4,5,2,3,1,5,5,1) I believe I need to use one of the apply functions in combination with which but I cannot make it work. thank you, -david __ R-help@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. __ R-help@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.
Re: [R] replacing values in one vector with corresponding index from another vector
thank you! that is straight forward. On Wed, 2011-10-19 at 22:37 +, William Dunlap wrote: match(b, a) [1] 4 5 2 3 1 5 5 1 __ R-help@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.
Re: [R] replacing values in a vector
What do you plan to do with those vectors? Creating a set of vectors and 'asign'ing them as global variables will likely make future tasks harder than they need to be. In the long run it will probably work better for you to either keep them as a matrix (and just access the row/column you need) or put them into a single list and access the elements of that. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Karen Kotschy Sent: Tuesday, August 17, 2010 4:37 PM To: Dimitris Rizopoulos Cc: r-help@r-project.org Subject: Re: [R] replacing values in a vector Thanks so much to all those who suggested solutions! You guys are wonderful! I went with Dimitris' suggestion in the end. Regards Karen On Tue 17Aug10, Dimitris Rizopoulos wrote: try this: vec - c(3,2,6,4,7) n - length(vec) for(i in seq_along(vec)){ r - numeric(n) r[i] - vec[i] assign(paste(vec., letters[i], sep = ), r) } I hope it helps. Best, Dimitris On 8/17/2010 12:57 PM, Karen Kotschy wrote: Dear helpRs Does anyone have an elegant way of doing the following: For a given numeric vector, e.g. vec- c(3,2,6,4,7) Create a series of vectors where all but 1 of the values are replaced by 0's, e.g. vec.a- c(3,0,0,0,0) vec.b- c(0,2,0,0,0) vec.c- c(0,0,6,0,0) vec.d- c(0,0,0,4,0) vec.e- c(0,0,0,0,7) I have looked at `replace', but can't think of a way of making it produce the 5 lines above without a for loop. I would also like to assign the names automatically. I can create them easily using paste, but how does one get R to treat the resulting character strings as object names to which values can be assigned? Thanks! Karen --- Karen Kotschy Centre for Water in the Environment University of the Witwatersrand, Johannesburg, South Africa -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. __ R-help@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. __ R-help@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.
[R] replacing values in a vector
Dear helpRs Does anyone have an elegant way of doing the following: For a given numeric vector, e.g. vec - c(3,2,6,4,7) Create a series of vectors where all but 1 of the values are replaced by 0's, e.g. vec.a - c(3,0,0,0,0) vec.b - c(0,2,0,0,0) vec.c - c(0,0,6,0,0) vec.d - c(0,0,0,4,0) vec.e - c(0,0,0,0,7) I have looked at `replace', but can't think of a way of making it produce the 5 lines above without a for loop. I would also like to assign the names automatically. I can create them easily using paste, but how does one get R to treat the resulting character strings as object names to which values can be assigned? Thanks! Karen --- Karen Kotschy Centre for Water in the Environment University of the Witwatersrand, Johannesburg, South Africa -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. __ R-help@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.
Re: [R] replacing values in a vector
Hi Karen, Try this: vec - c(3,2,6,4,7) res - diag(vec) k - length(vect) for(i in 1:k) assign(paste('vec', letters[i], sep = '.'), res[i,]) vec.a # [1] 3 0 0 0 0 vec.b # [1] 0 2 0 0 0 HTH, Jorge On Tue, Aug 17, 2010 at 6:57 AM, Karen Kotschy wrote: Dear helpRs Does anyone have an elegant way of doing the following: For a given numeric vector, e.g. vec - c(3,2,6,4,7) Create a series of vectors where all but 1 of the values are replaced by 0's, e.g. vec.a - c(3,0,0,0,0) vec.b - c(0,2,0,0,0) vec.c - c(0,0,6,0,0) vec.d - c(0,0,0,4,0) vec.e - c(0,0,0,0,7) I have looked at `replace', but can't think of a way of making it produce the 5 lines above without a for loop. I would also like to assign the names automatically. I can create them easily using paste, but how does one get R to treat the resulting character strings as object names to which values can be assigned? Thanks! Karen --- Karen Kotschy Centre for Water in the Environment University of the Witwatersrand, Johannesburg, South Africa -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] replacing values in a vector
Try this: mapply(function(x, y)assign(x, y, envir = globalenv()), sprintf('vec.%s', letters[1:length(vec)]), split(diag(vec), 1:length(vec))) On Tue, Aug 17, 2010 at 7:57 AM, Karen Kotschy ka...@sevenc.co.za wrote: Dear helpRs Does anyone have an elegant way of doing the following: For a given numeric vector, e.g. vec - c(3,2,6,4,7) Create a series of vectors where all but 1 of the values are replaced by 0's, e.g. vec.a - c(3,0,0,0,0) vec.b - c(0,2,0,0,0) vec.c - c(0,0,6,0,0) vec.d - c(0,0,0,4,0) vec.e - c(0,0,0,0,7) I have looked at `replace', but can't think of a way of making it produce the 5 lines above without a for loop. I would also like to assign the names automatically. I can create them easily using paste, but how does one get R to treat the resulting character strings as object names to which values can be assigned? Thanks! Karen --- Karen Kotschy Centre for Water in the Environment University of the Witwatersrand, Johannesburg, South Africa -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. __ R-help@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 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] replacing values in a vector
Hi, You can use diag() and matrix multiplication to create a matrix with the requested rows, and assign() to store the rows as separate vectors: vec - c(3,2,6,4,7) mat - diag(vec) for (i in seq_along(vec)) assign(paste(vec, letters[i], sep=.), mat[i,]) vec.a [1] 3 0 0 0 0 vec.b [1] 0 2 0 0 0 Best regards, Charlie Roosen Mango Solutions -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Karen Kotschy Sent: 17 August 2010 12:58 To: r-help@r-project.org Subject: [R] replacing values in a vector Dear helpRs Does anyone have an elegant way of doing the following: For a given numeric vector, e.g. vec - c(3,2,6,4,7) Create a series of vectors where all but 1 of the values are replaced by 0's, e.g. vec.a - c(3,0,0,0,0) vec.b - c(0,2,0,0,0) vec.c - c(0,0,6,0,0) vec.d - c(0,0,0,4,0) vec.e - c(0,0,0,0,7) I have looked at `replace', but can't think of a way of making it produce the 5 lines above without a for loop. I would also like to assign the names automatically. I can create them easily using paste, but how does one get R to treat the resulting character strings as object names to which values can be assigned? Thanks! Karen --- Karen Kotschy Centre for Water in the Environment University of the Witwatersrand, Johannesburg, South Africa -- This message has been scanned for viruses and\ dangerous...{{dropped:20}} __ R-help@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.
Re: [R] replacing values in a vector
try this: vec - c(3,2,6,4,7) n - length(vec) for(i in seq_along(vec)){ r - numeric(n) r[i] - vec[i] assign(paste(vec., letters[i], sep = ), r) } I hope it helps. Best, Dimitris On 8/17/2010 12:57 PM, Karen Kotschy wrote: Dear helpRs Does anyone have an elegant way of doing the following: For a given numeric vector, e.g. vec- c(3,2,6,4,7) Create a series of vectors where all but 1 of the values are replaced by 0's, e.g. vec.a- c(3,0,0,0,0) vec.b- c(0,2,0,0,0) vec.c- c(0,0,6,0,0) vec.d- c(0,0,0,4,0) vec.e- c(0,0,0,0,7) I have looked at `replace', but can't think of a way of making it produce the 5 lines above without a for loop. I would also like to assign the names automatically. I can create them easily using paste, but how does one get R to treat the resulting character strings as object names to which values can be assigned? Thanks! Karen --- Karen Kotschy Centre for Water in the Environment University of the Witwatersrand, Johannesburg, South Africa -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ R-help@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.
Re: [R] replacing values in a vector
Thanks so much to all those who suggested solutions! You guys are wonderful! I went with Dimitris' suggestion in the end. Regards Karen On Tue 17Aug10, Dimitris Rizopoulos wrote: try this: vec - c(3,2,6,4,7) n - length(vec) for(i in seq_along(vec)){ r - numeric(n) r[i] - vec[i] assign(paste(vec., letters[i], sep = ), r) } I hope it helps. Best, Dimitris On 8/17/2010 12:57 PM, Karen Kotschy wrote: Dear helpRs Does anyone have an elegant way of doing the following: For a given numeric vector, e.g. vec- c(3,2,6,4,7) Create a series of vectors where all but 1 of the values are replaced by 0's, e.g. vec.a- c(3,0,0,0,0) vec.b- c(0,2,0,0,0) vec.c- c(0,0,6,0,0) vec.d- c(0,0,0,4,0) vec.e- c(0,0,0,0,7) I have looked at `replace', but can't think of a way of making it produce the 5 lines above without a for loop. I would also like to assign the names automatically. I can create them easily using paste, but how does one get R to treat the resulting character strings as object names to which values can be assigned? Thanks! Karen --- Karen Kotschy Centre for Water in the Environment University of the Witwatersrand, Johannesburg, South Africa -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. __ R-help@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.
[R] replacing values in a vector
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 __ R-help@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.
Re: [R] replacing values in a vector
Boy are you confused. This has nothing at all to do with substitution. Instead do test - with(fc,ave.fc[match(diff_mirs_list,Probe)]) cheers, Rolf Turner On 7/11/2008, at 11:46 AM, Iain Gallagher wrote: 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. ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ R-help@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.