I would like to combine three columns in one of my data sets into one with variable name “al_anim” and remove any duplicates, rank the values (animal ids) from lowest to highest, and re-number each animal from 1 to N under the variable name “new_id”.
anim1 <- c(1456,2569,5489,1456,4587)
anim2 <- c(6531,6987,6987,15487,6531)
anim3 <- c(4587,6548,7894,3215,8542)
mydf <- data.frame(anim1,anim2,anim3)
Any help would be very much appreciated!
Baz
Using
mydffrom your example:Stack the data:
Then compute the unique elements using
unique()and then this will get them a new animal id
which would give:
However that is totally trivial;
seq_along(uni)gets you there far more easily. So I wonder if you wantwhich gives:
There is an amount of ambiguity in your Question which could be alleviated by giving an expected result/output.