Re: [R] factor manipulation: edgelist to a matrix?

From: Veslot Jacques <jacques.veslot_at_cemagref.fr>
Date: Thu, 20 Dec 2007 08:53:41 +0100

do.call(rbind,lapply(split(as.character(z[,2]),z[,1]), function(x) c(x, rep(NA, max(table(z[,1]))-length(x)))))

Jacques VESLOT

CEMAGREF - UR Hydrobiologie

Route de Cézanne - CS 40061
13182 AIX-EN-PROVENCE Cedex 5, France

Tél + 0033 04 42 66 99 76
email jacques.veslot_at_cemagref.fr

>-----Message d'origine-----
>De : r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] De
>la part de Christopher Marcum
>Envoyé : jeudi 20 décembre 2007 08:33
>À : r-help_at_stat.math.ethz.ch
>Objet : [R] factor manipulation: edgelist to a matrix?
>
>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
>
>______________________________________________
>R-help_at_r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-
>guide.html
>and provide commented, minimal, self-contained, reproducible code.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 20 Dec 2007 - 07:57:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 20 Dec 2007 - 09:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.