Pasting elements of two vectors alphabetically
slightly redundant because it sorts twice, but vectorised,
paste(pmin(a,b), pmax(a,b))
Edit: alternative with ifelse
,
ifelse(a < b, paste(a, b), paste(b, a))
Here's one approach:
apply(cbind(a, b), 1, function(x) paste(sort(x), collapse=" "))
## [1] "george harry" "harry steve" "chris harry" "chris harry"
## [5] "harry steve" "george steve" "chris steve" "george harry"
Using your initial attempt, you could also do the following but they both require more typing (not sure about speed):
unlist(Map(function(x, y) paste(sort(c(x, y)), collapse=" "), a, b),,FALSE)
mapply(function(x, y) paste(sort(c(x, y)), collapse=" "), a, b, USE.NAMES = FALSE)
One liner from your own code:
apply(data.frame(apply(mapply(c, a, b, USE.NAMES = FALSE),1,paste)),1,function(x) paste(x[1],x[2]))
[1] "george harry" "harry steve" "harry chris" "chris harry" "steve harry" "steve george" "steve chris" "harry george"
apply(apply(mapply(c, a, b, USE.NAMES = FALSE),2,sort),1,paste)
[,1] [,2]
[1,] "george" "harry"
[2,] "harry" "steve"
[3,] "chris" "harry"
[4,] "chris" "harry"
[5,] "harry" "steve"
[6,] "george" "steve"
[7,] "chris" "steve"
[8,] "george" "harry"