One way:
colSums(combn(shapes, 2, FUN=nchar))
# [1] 14 13 13 15 15 14
Benchmarks:
shapes <- rep(c("Square", "Triangle","Octagon","Hexagon"), 50)
matthew <- function() colSums(combn(shapes, 2, FUN=nchar))
ananda <- function() combn(shapes, 2, FUN=function(x) sum(nchar(x)))
tstenner <- function() {
shapelengths <- nchar(shapes)
colSums(combn(shapelengths, 2))
}
microbenchmark(matthew(), ananda(), tstenner())
# Unit: milliseconds
# expr min lq median uq max neval
# matthew() 53.27 55.84 56.37 57.04 98.83 100
# ananda() 74.74 78.42 79.88 80.60 123.29 100
# tstenner() 29.27 31.03 31.52 33.44 76.01 100