Is there a way to concatenate the row and column names from an existing data.frame into a new data frame. For example, I have column names of (A, B, C) and row names of (1, 2, 3) and I would like to combine these into a 3x3 matrix [A1, B1, C1; A2, B2, C2; A2, B2, C2]. Thanks for your help
+7
A:
The outer()
function can help:
> cn <- c("A","B","C")
> rn <- c("1","2","3")
> outer(cn, rn, function(x,y) paste(x,y,sep=""))
[,1] [,2] [,3]
[1,] "A1" "A2" "A3"
[2,] "B1" "B2" "B3"
[3,] "C1" "C2" "C3"
>
Dirk Eddelbuettel
2010-05-11 20:51:13
Perfect--thanks!
2010-05-11 21:06:32
Or short version `outer(cn, rn, paste, sep="")`
Marek
2010-05-11 22:04:23
This is one of those moments when I bless the existence of `...` function argument!
aL3xa
2010-05-11 23:26:01