[R] factor manipulation: edgelist to a matrix?

Christopher Marcum cmarcum at uci.edu
Thu Dec 20 08:33:15 CET 2007


Hello All,

I have had considerable bad luck with attempting the following with for
loops. Here is the problem:


#	Suppose we have a data.frame with the following data, which can be
considered a type of edgelist (for those with networks backgrounds):
#
#       V1	V2
#	1	A
#	1	A
#	1	B
#	2	A
#	3	C
#	3	A
#	3	C
#	3	B
#
#	I want the output of the function to produce a matrix, such that #each
factor of V1 is a row, and each corresponding value at position k of V2 is
the i,j^th element of the new matrix, with missing values otherwise. The
desired output should be:
#	[,1]	[,2]	[,3]	[,4]
# [1,]	A	A	B	NA
# [2,]	A	NA	NA	NA
# [3,]	C	A	C	B

I have explored the reshape package as well as the network package in this
pursuit, with no luck.

Thanks,
Chris Marcum
UCI Sociology



More information about the R-help mailing list