You can do this with the help of clever (so to speak) manipulations with transposition to factors :
stuff[order(factor(sapply(stuff,function(x)unlist(strsplit(x,''))[1]),levels=pos))]
edited to add some clarification and simplify:
The first part will be to isolate only the first character of your material vector. This can be done with:
> sapply(stuff,function(x)unlist(strsplit(x,''))[1]) 3S AH 2I B4 AL "3" "A" "2" "B" "A"
You can then convert this to factors using the pos vector as levels:
> factor(sapply(stuff,function(x)unlist(strsplit(x,''))[1]),levels=pos) 3S AH 2I B4 AL 3 A 2 BA Levels: 3 BA 2
Finally, you can get the correct order from these factors:
> order(factor(sapply(stuff,function(x)unlist(strsplit(x,''))[1]),levels=pos)) [1] 1 4 2 5 3
The finish line just takes a subset of your original stuff vector:
> stuff[order(factor(sapply(stuff,function(x)unlist(strsplit(x,''))[1]),levels=pos))] [1] "3S" "B4" "AH" "AL" "2I"
source share