[R] reshaping a dataset for a network

R. Michael Weylandt michael.weylandt at gmail.com
Wed Mar 14 13:54:44 CET 2012


You can't have "empty" spots like that in an array. One choice would
be to fill them with NAs:

library(plyr)
do.call(rbind.fill.matrix,lapply(split(b,a), t))

Michael

On Wed, Mar 14, 2012 at 4:01 AM, Marco Guerzoni <marco.guerzoni at unito.it> wrote:
> Thank you fro the reply.
> I managed to arrive till here, then I would like to have it in matrix where
> the $1 $2...$5 are the first column.
>
> Il 3/13/2012 8:15 PM, William Dunlap ha scritto:
>
>> Is the following what you want?
>>   >  a<- c(1,2,3,4,4,4,5,5)
>>   >  b<- c(11,7,4,9,8,3,12,4)
>>   >  split(b, a)
>>   $1
>>   [1] 11
>>
>>   $2
>>   [1] 7
>>
>>   $3
>>   [1] 4
>>
>>   $4
>>   [1] 9 8 3
>>
>>   $5
>>   [1] 12  4
>
>
> Here I did
>
> c <- split(b, a)
> d <- do.call(rbind,c)
>
> and I get
>
>  [,1] [,2] [,3]
> 1   11   11   11
> 2    7    7    7
> 3    4    4    4
> 4    9    8    3
> 5   12    4   12
>
>
> Instead of what I would like:
>
>  [,1] [,2] [,3]
>
> 1   11
> 2    7
> 3    4
> 4    9    8    3
> 5   12    4
>
> A solution could be rbind.fill , which does not seem to work with list.
>
>
> thanks
> Marco Guerzoni,
> Department of Economics
> University of Turin
>
>
>
>>
>> Note that your df<-cbind(a,b) produces a matrix, not the data.frame
>> that your df suggests you want.  Use df<-data.frame(a,b) to make
>> a data.frame.  Then you could do with(df, split(a,b)) to operate on
>> the a and b in the data.frame df.
>>
>> Bill Dunlap
>> Spotfire, TIBCO Software
>> wdunlap tibco.com
>>
>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>>> On Behalf
>>> Of Marco Guerzoni
>>> Sent: Tuesday, March 13, 2012 10:51 AM
>>> To: r-help at r-project.org
>>> Subject: [R] reshaping a dataset for a network
>>>
>>> dear all,
>>> apologizes for bothering with a probably stupid question but I really
>>> don' t know how to proceed.
>>>
>>> I have a dataset which look like df
>>>
>>> a<- c(1,2,3,4,4,4,5,5)
>>> b<- c(11,7,4,9,8,3,12,4)
>>> df<-cbind(a,b)
>>>
>>> I would like to have one which looks like this:
>>>
>>> a
>>> 1 11
>>> 2 7
>>> 3 4
>>> 4 9 8 3
>>> 5 12 4
>>>
>>> a are vertex of a network, b the edges. In the data the lenght of a is
>>> about 50000
>>>
>>> I read several posts about reshape, reshape2, split, ldply but I
>>> couldn't manage to do it. The problem seems to be that the is not a real
>>> panel.
>>>
>>> Any help would be really appreciated,
>>> my best regards
>>> Marco
>>>
>>> ______________________________________________
>>> 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.



More information about the R-help mailing list