Creating a Matrix From a Vector in R

I have a vector with two columns, one column containing numerical values and one column containing names.I'm a novice to R but essentially I want to take a vector and create a matrix with it wherein the values within the matrix would add together. So for example, where the vector A has a value of 1 and B has a value of 1, in the matrix at the intersection of A and B I want the values to add and become 2.

I've tried to use a for loop but I'm having trouble with the arguments to put within the loop. Any help would be greatly appreciated and I'd be glad to clarify stuff if it doesn't make sense.

Essentially what I want is to take this: A 1 B 0 C 0 D 1 And turn it into this:

   A   B   C   D
A      1   1   2
B  1       0   1
C  1   0       1
D  2   1   1         

Thanks!

Answers


R > x <- c(1,0,0,1)
R > outer(x, x, "+")
     [,1] [,2] [,3] [,4]
[1,]    2    1    1    2
[2,]    1    0    0    1
[3,]    1    0    0    1
[4,]    2    1    1    2

The next thing is to ignore the diagonal. Updated by Vincent:

names(x) <- c("A","B","C","D") 

Need Your Help

How do i implement this scenario using PHP?

php parallel-processing

I am writing an android application which uses C2DM. When a user sends some message to the server, the server replies back to the sender and forwards that message to a set of other users in the net...

Rails active_resource require JS to do delete by link_to?

javascript ruby-on-rails ruby activeresource

When I use link_to :method=>:delete, what rails has generated is a javascript click, which is not nice if you do come across situation your client just have javascript disabled. That seems a bad id...