According to ?unique
:
‘unique’ returns a vector, data frame or array like ‘x’ but with
duplicate elements/rows removed.
This description gives you a complete description of the ordering -- it will be in the same order as the order of the first unique elements. (I guess I don't see the wiggle room that @joran sees for a different ordering.) For example,
unique(c("B","B","A","C","C","C","B","A"))
will result in
[1] "B" "A" "C"
I believe unique(x)
will in general be identical to (but more efficient than)
x[!duplicated(x)]
If you want to look at the internal code, see here: the moving parts are something like
k = 0;
switch (TYPEOF(x)) {
case LGLSXP:
case INTSXP:
for (i = 0; i < n; i++)
if (LOGICAL(dup)[i] == 0)
INTEGER(ans)[k++] = INTEGER(x)[i];
break;
i.e., the internal representation is exactly what I said, that it goes through the vector sequentially and fills in non-duplicated elements. Since ordering isn't explicitly guaranteed in the documentation it is theoretically possible that this implementation could change in the future, but it is almost vanishingly unlikely.
For what you're trying to do there are simpler R idioms
df <- data.frame(group=rep(LETTERS, each=2), value=1:52)
a1 <- aggregate(df$value,list(df$group),mean)
This returns a two-column data frame, so you can use
setNames(a1[,2],a1[,1])
to convert it to your format. Or
library(plyr)
unlist(daply(df,"group",summarise,val=mean(value)))