[R] organizing data in a matrix avoiding loop

A M Lavezzi mario.lavezzi at unipa.it
Fri May 26 18:40:06 CEST 2017


Dear Sarah
thank you very much. I used "crosstab" and it worked,

xxx<-crosstab(dataTrade$iso_o,dataTrade$iso_d,dataTrade$FLOW,type="sum",na.rm=TRUE)

All the best
Mario

On Fri, May 26, 2017 at 2:15 PM, Sarah Goslee <sarah.goslee at gmail.com>
wrote:

> There are various ways to do this. It shouldn't take forever as a loop,
> with only 215 entries.
>
> I find crosstab() from the ecodist package helpful. The current version is
> on GitHub, but not yet CRAN (soon!).
>
> Sarah
>
> On Fri, May 26, 2017 at 7:47 AM A M Lavezzi <mario.lavezzi at unipa.it>
> wrote:
>
>> Dear R-Users
>>
>> I have data on bilateral trade flows among countries in the following
>> form:
>>
>> > head(dataTrade)
>>
>>       iso_o iso_d year FLOW
>> 1   ABW   AFG 1985   NA
>> 2   ABW   AFG 1986   NA
>> 3   ABW   AFG 1987   NA
>> 4   ABW   AFG 1988   NA
>> 5   ABW   AFG 1989   NA
>> 6   ABW   AFG 1990   NA
>>
>> where:
>> iso_o: code of country of origin
>> iso_d: code of country of destination
>> year: 1985:2015
>> FLOW: amount of trade (values are "NA", 0s, or positive numbers)
>
>
>
>> I have 215 countries. I would like to create a 215x215 matrix , say M, in
>> which element M(i,j) is the total trade between countries i and j between
>> 1985 and 2015 (i.e. the sum of annual amounts of trade).
>>
>> After collecting the country codes in a variable named "my_iso", I can
>> obtain M in a straightforward way using a loop such as:
>>
>> for (i in my_iso){
>>   for(j in my_iso)
>>     if(i!=j){
>>       M[seq(1:length(my_iso))[my_iso==i],seq(1:length(my_iso))[
>> my_iso==j]]
>> <-
>>         sum(dataTrade[dataTrade$iso_o==i &
>> dataTrade$iso_d==j,"FLOW"],na.rm=TRUE)
>>     }
>> }
>>
>> However, it takes ages.
>>
>> Is there a way to avoid these loops?
>>
>> Thanks for your help
>> Mario
>>
>>
>> --
>> Andrea Mario Lavezzi
>> DiGi,Sezione Diritto e Società
>> Università di Palermo
>> Piazza Bologni 8
>> 90134 Palermo, Italy
>> tel. ++39 091 23892208 <+39%20091%202389%202208>
>> fax ++39 091 6111268 <+39%20091%20611%201268>
>> skype: lavezzimario
>> email: mario.lavezzi (at) unipa.it
>> <https://urlsand.esvalabs.com/?u=http%3A%2F%2Funipa.it&e=ae0ec65b&h=e13e0f75&f=y>
>> web: http://www.unipa.it/~mario.lavezzi
>> <https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.unipa.it%2F~mario.lavezzi&e=ae0ec65b&h=f8f16f65&f=y>
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> <https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=ae0ec65b&h=8af6ace6&f=y>
>> PLEASE do read the posting guide http://www.R-project.org/
>> posting-guide.html
>> <https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=ae0ec65b&h=ea1999c9&f=y>
>> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Sarah Goslee
> http://www.stringpage.com
> <https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.stringpage.com&e=ae0ec65b&h=19bbc433&f=y>
> http://www.sarahgoslee.com
> <https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.sarahgoslee.com&e=ae0ec65b&h=5b5cfa0d&f=y>
> http://www.functionaldiversity.org
> <https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.functionaldiversity.org&e=ae0ec65b&h=f81071c1&f=y>
>



-- 
Andrea Mario Lavezzi
DiGi,Sezione Diritto e Società
Università di Palermo
Piazza Bologni 8
90134 Palermo, Italy
tel. ++39 091 23892208
fax ++39 091 6111268
skype: lavezzimario
email: mario.lavezzi (at) unipa.it
web: http://www.unipa.it/~mario.lavezzi

	[[alternative HTML version deleted]]



More information about the R-help mailing list