Re: [R] perfectionism

2011-11-25 Thread Barth B. Riley
-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(

Re: [R] perfectionism

2011-11-25 Thread Jack Tanner
jim holtman 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

Re: [R] perfectionism

2011-11-25 Thread Patrick Burns
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

Re: [R] perfectionism

2011-11-25 Thread jim holtman
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 wrote: > I have a named vector: > >> z <- c(1, 2, 3, 2) >>

[R] perfectionism

2011-11-25 Thread Jack Tanner
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 i