I didn't really know what to post as the topic subject, but I have a vector, for instance (2,2,4,6,2,4,4,6,8,6) and I want to create another vector which is just numbers from 1 to 4 since there are only 4 unique numbers in my vector, so for instance 2 would be 1, 4 would be 2, 6 would be 3, and 8 would be 4, so my new vector would be (1,1,2,3,1,2,2,3,4,3). The vector I have has longer numbers, in the thousands, but it's the same principle I want. I have a vector of length 200 but I know there are only 160 unique numbers in there, so would like a new vector that just has numbers between 1 and 160 correpsonding to each unique element of the original vector, and is the same length as the original. Sorry if this is not very clear, Thanks very much for any help in advance. -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2321440.html Sent from the R help mailing list archive at Nabble.com.
Hi, Try ?unique please. x <- c(2,2,9,4,6,2,4,4,6,8,6) # Original vector unique(x) #New vector only has unique elements sort(unique(x)) # Ordered Regards, Wu ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2321884.html Sent from the R help mailing list archive at Nabble.com.
clips10 <m.mcquillan <at> lancaster.ac.uk> writes:> I didn't really know what to post as the topic subject, but I have a vector, > for instance (2,2,4,6,2,4,4,6,8,6) [... snip to make gmane happy ...], > so my new vector would be (1,1,2,3,1,2,2,3,4,3).x <- c(2,2,4,6,2,4,4,6,8,6) as.numeric(factor(x)) [1] 1 1 2 3 1 2 2 3 4 3
Here's another way... x <- c(2,2,4,6,2,4,4,6,8,6) match(x, unique(x)) Produces... [1] 1 1 2 3 1 2 2 3 4 3 On 12 August 2010 01:48, clips10 <m.mcquillan at lancaster.ac.uk> wrote:> > I didn't really know what to post as the topic subject, but I have a vector, > for instance (2,2,4,6,2,4,4,6,8,6) and I want to create another vector which > is just numbers from 1 to 4 since there are only 4 unique numbers in my > vector, so for instance 2 would be 1, 4 would be 2, 6 would be 3, and 8 > would be 4, so my new vector would be
Thanks for the help, I tried to apply this to a vector with two columns, well I suppose it is not a vector but for instance like this: [,1] [,2] [1,] 1 2 [2,] 2 3 [3,] 1 2 [4,] 1 2 [5,] 3 4 and return a vector : 1,2,1,1,3, so that it recognises both columns together. I tried match(x, unique(x)) as earlier suggested but this returns a vector of length 10 as opposed to 5, even though unique(x) does remove the repeated rows. Sorry if this is confusing, I am trying to do as originally posted but with 2 columns Thanks -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2322646.html Sent from the R help mailing list archive at Nabble.com.
I think your code will work but only for the two columns I gave. I used those as an example but my actual data is 200 in length with two columns and I need code that will give a label to each unique pair but still have the original length for instance, one that will turn something such as [,1] [,2] [1,] 1 2 [2,] 2 3 [3,] 1 2 [4,] 4 6 [5,] 23 1 into one vector of length 5, giving a number for each unique pair (1,2,1,3,4) so that the same pairs get the same number. but my actual data is of length 200 with two columns Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Creating-vectors-tp2321440p2322933.html Sent from the R help mailing list archive at Nabble.com.