Set column names while calling a function
Consider we have a numeric data.frame foo and want to find the sum of each two columns: foo <- data.frame(x=1:5,y=4:8,z=10:14, w=8:4) bar <- combn(colnames(foo), 2, function(x) foo[,x[1]] + foo[,x[2]]) bar # [,1] [,2] [,3] [,4] [,5] [,6] #[1,] 5 11 9 14 12 18 #[2,] 7 13 9 16 12 18 #[3,] 9 15 9 18 12 18 #[4,] 11 17 9 20 12 18 #[5,] 13 19 9 22 12 18 Everything is fine, except the column names that are missing from bar . I want column names of bar to show the related columns in foo , for instance in this example: colnames(bar) <- apply(combn(colnames(foo),2), 2, paste0,collapse="") colnames(bar)