[R] select subset based on another variable
Hi, dear R-helpers, I have a simple question regarding selecting subset of a variable based on another variable. Here is the example: xx=rnorm(10) id=sample(1:10, 10) temp=c(6, 1, 8, 2) Now, all I want is xx's that their id are 6, 1, 8, 2, instead of the position. Any suggestions ? Thank you all your help !! Carrie [[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] select subset based on another variable
On May 13, 2010, at 9:48 AM, Carrie Li wrote: Hi, dear R-helpers, I have a simple question regarding selecting subset of a variable based on another variable. Here is the example: xx=rnorm(10) id=sample(1:10, 10) temp=c(6, 1, 8, 2) Now, all I want is xx's that their id are 6, 1, 8, 2, instead of the position. Any suggestions ? xx[temp] -- David. Thank you all your help !! Carrie [[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. David Winsemius, MD West Hartford, CT __ 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] select subset based on another variable
Try this: subset(xx, id %in% temp) On Thu, May 13, 2010 at 10:48 AM, Carrie Li carrieands...@gmail.com wrote: Hi, dear R-helpers, I have a simple question regarding selecting subset of a variable based on another variable. Here is the example: xx=rnorm(10) id=sample(1:10, 10) temp=c(6, 1, 8, 2) Now, all I want is xx's that their id are 6, 1, 8, 2, instead of the position. Any suggestions ? Thank you all your help !! Carrie [[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. -- 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] select subset based on another variable
try this: xx=rnorm(10) id=sample(1:10, 10) temp=c(6, 1, 8, 2) xx[match(temp, id)] [1] 0.4874291 -0.6212406 1.5117812 0.5757814 xx [1] -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884 1.5117812 0.3898432 -0.6212406 -2.2146999 [10] 1.1249309 id [1] 5 6 4 2 10 8 9 1 7 3 On Thu, May 13, 2010 at 9:48 AM, Carrie Li carrieands...@gmail.com wrote: Hi, dear R-helpers, I have a simple question regarding selecting subset of a variable based on another variable. Here is the example: xx=rnorm(10) id=sample(1:10, 10) temp=c(6, 1, 8, 2) Now, all I want is xx's that their id are 6, 1, 8, 2, instead of the position. Any suggestions ? Thank you all your help !! Carrie [[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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[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] select subset based on another variable
On May 13, 2010, at 9:55 AM, David Winsemius wrote: On May 13, 2010, at 9:48 AM, Carrie Li wrote: Hi, dear R-helpers, I have a simple question regarding selecting subset of a variable based on another variable. Here is the example: xx=rnorm(10) id=sample(1:10, 10) temp=c(6, 1, 8, 2) Now, all I want is xx's that their id are 6, 1, 8, 2, instead of the position. Any suggestions ? xx[temp] I may have jumped the gun, but on reflection I am not sure that your question was at all clear. Is the first item desired the 6th item of xx or the xx item referenced by the 6th item of id? You gave a specification that would not allow any of us to check, since you did not specify a random seed. We have no way of seeing what either of your xx or id vectors look like since you did not provide either them or the desired output. If it were the second specification then consider this: set.seed(123) xx=rnorm(10) id=sample(1:10, 10) temp=c(6, 1, 8, 2) xx;id [1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 [7] 0.46091621 -1.26506123 -0.68685285 -0.44566197 [1] 9 7 6 10 4 8 3 2 1 5 xx[id[temp]] [1] -1.2650612 -0.6868529 -0.2301775 0.4609162 -- David. Thank you all your help !! Carrie [[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. David Winsemius, MD West Hartford, CT __ 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. David Winsemius, MD West Hartford, CT __ 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.