[R] perfectionism
I have a named vector: z - c(1, 2, 3, 2) names(z) - c(a,b,c,b) f - c(b,c) I want to know the index in z of the first occurrence of each of the values in f. One implementation is sapply(f, function(x) which(names(z)==x)[1]) b c 2 3 Is which() smart enough to stop when it finds in z the first occurrence of every value from f, or does it search through all the values in z only to report the first one? Are some more elegant ways of writing this code? Just curious. __ 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] perfectionism
Try this: z - c(1, 2, 3, 2) names(z) - c(a,b,c,b) f - c(b,c) match(f, names(z)) [1] 2 3 # if you want the names x - match(f, names(z)) names(x) - f x b c 2 3 On Fri, Nov 25, 2011 at 2:23 PM, Jack Tanner i...@hotmail.com wrote: I have a named vector: z - c(1, 2, 3, 2) names(z) - c(a,b,c,b) f - c(b,c) I want to know the index in z of the first occurrence of each of the values in f. One implementation is sapply(f, function(x) which(names(z)==x)[1]) b c 2 3 Is which() smart enough to stop when it finds in z the first occurrence of every value from f, or does it search through all the values in z only to report the first one? Are some more elegant ways of writing this code? Just curious. __ 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. __ 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] perfectionism
Did you try: z[f] On 25/11/2011 19:23, Jack Tanner wrote: I have a named vector: z- c(1, 2, 3, 2) names(z)- c(a,b,c,b) f- c(b,c) I want to know the index in z of the first occurrence of each of the values in f. One implementation is sapply(f, function(x) which(names(z)==x)[1]) b c 2 3 Is which() smart enough to stop when it finds in z the first occurrence of every value from f, or does it search through all the values in z only to report the first one? Are some more elegant ways of writing this code? Just curious. __ 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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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] perfectionism
jim holtman jholtman at gmail.com writes: match(f, names(z)) [1] 2 3 Jim, thanks so much, that's right on. Patrick, thanks to you too, but yours is not the same as what I asked: z - c(3,4,5,4) names(z)- c(a,b,c,b) z[f] b c 4 5 Yours returns the actual values in z, not the indexes in z, i.e., not [1] 2 3 __ 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] perfectionism
Z[f] would return the fth values in z. That is, if f = c(1,2) z[f] would return the first and second elements of z. The original intent was to obtain the indices in z corresponding to the values in f. Barth -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Patrick Burns Sent: Friday, November 25, 2011 1:41 PM To: r-help@r-project.org; i...@hotmail.com Subject: Re: [R] perfectionism Did you try: z[f] On 25/11/2011 19:23, Jack Tanner wrote: I have a named vector: z- c(1, 2, 3, 2) names(z)- c(a,b,c,b) f- c(b,c) I want to know the index in z of the first occurrence of each of the values in f. One implementation is sapply(f, function(x) which(names(z)==x)[1]) b c 2 3 Is which() smart enough to stop when it finds in z the first occurrence of every value from f, or does it search through all the values in z only to report the first one? Are some more elegant ways of writing this code? Just curious. __ 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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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. PRIVILEGED AND CONFIDENTIAL INFORMATION This transmittal and any attachments may contain PRIVILEGED AND CONFIDENTIAL information and is intended only for the use of the addressee. If you are not the designated recipient, or an employee or agent authorized to deliver such transmittals to the designated recipient, you are hereby notified that any dissemination, copying or publication of this transmittal is strictly prohibited. If you have received this transmittal in error, please notify us immediately by replying to the sender and delete this copy from your system. You may also call us at (309) 827-6026 for assistance. __ 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.