[S] naming unique entries in a vector

Douglas Bates (bates@stat.wisc.edu)
14 Apr 1998 12:22:44 -0500


srosenfeld@nesdis.noaa.gov writes:

> Can anybody help me in solving the following problem:
> I have a series of complex numbers z[i], i=1,....,N
> There are numerous repetitions in these numbers. For example, N is of order
> 1,000,000. However, only ~20,000 of z[i] are different. I need to assign a
> unique name or number to each of z[i] in such a way that equal z have the same
> name.
> For example, I have
>
> z_c(0+0i,1+0i,0+1i,1+1i,10+1i,0+1i,0+1i,1+1i,10-1i,0+1i,10+1i,0+1i,0+1i,1+1i)
>
> I need to name them as follows
> names(z)_c("z1","z2","z3","z4","z5","z3","z3","z4","z6","z3","z5","z3","z3","z4"
> )

There is a meta-answer to many of the questions proposed on this list
and that is "use the match function and indexing".

Here is how it would work in this case:

> z <- c(0+0i,1+0i,0+1i,1+1i,10+1i,0+1i,0+1i,1+1i,10-1i,0+1i,10+1i,0+1i,0+1i,1+1i)
> uz <- unique( z )
> names(z) <- paste("z", seq(along = uz), sep = "")[ match( z, uz ) ]
> names(z)
[1] "z1" "z2" "z3" "z4" "z5" "z3" "z3" "z4" "z6" "z3" "z5" "z3"
[13] "z3" "z4"

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news