Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread A M Lavezzi
Hi David
thanks a lot for your suggestion. I followed the suggestion of Sarah (the
first on the thread) and solved my problem

I will keep into account you suggestion anyway
Mario

On Fri, May 26, 2017 at 4:51 PM, David L Carlson <dcarl...@tamu.edu> wrote:

> How about?
>
> Trade <- xtabs(FLOW ~ iso_o + iso_d + year, dta)
>
> Gives you a 3d table with FLOW as the cell entry. Then
>
> apply(Trade, 1:2, sum, na.rm=TRUE)
>
> Gives you a 2d table with the total flow
>
>
> David L. Carlson
> Department of Anthropology
> Texas A University
>
> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of S Ellison
> Sent: Friday, May 26, 2017 8:28 AM
> To: A M Lavezzi <mario.lave...@unipa.it>; r-help <r-help@r-project.org>
> Subject: Re: [R] organizing data in a matrix avoiding loop
>
> > -Original Message-
> > From: A M > Lavezzi
> >
> > I have data on bilateral trade flows among countries in the following
> form:
> >
> >   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
> >
> >...
> >
> > 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
> >
> > Is there a way to avoid these loops?
>
> Using core R:
> #Use aggregate() to aggregate across years:
>
> https://urlsand.esvalabs.com/?u=http%3A%2F%2FdataTrade.ag=
> ae0ec65b=cb58f304=y <- aggregate (dataTrade[,'Flow',drop=FALSE],
> by=dataTrade[, c('iso_o', 'iso_d')], FUN=sum, na.rm=TRUE)
>
> #where na.rm=TRUE (passed to sum()) essentially treats NAs as 0. If you
> really want NA leave it out or set it to FALSE
> #This gives you one row per origin/destination pair that contains the
> total trade in Flow.
> #If the years you want are a subset, subset the data frame first.
>
> #Form an empty matrix with suitable dimnames:
> N_iso <- length(my_iso)
> dT.m <- matrix(rep(NA, N_iso*N_iso), ncol=N_iso, dimnames=list(my_iso,
> my_iso))
>
> #Then use matrix indexing by name to populate your matrix with the
> available flow data
> dT.m[as.matrix(dataTrade.ag[1:2]) ] <- dataTrade.ag$Flow
> #This relies on a default conversion from data frame factors to a
> character matrix, together
> #with R's facility for matrix indexing by 2-column matrix
>
> #Then
> dataTrade.ag[1:10, 1:10]
>
> #should have what you seem to want
>
>
> S Ellison
>
>
>
>
> ***
> This email and any attachments are confidential. Any use, copying or
> disclosure other than by the intended recipient is unauthorised. If
> you have received this message in error, please notify the sender
> immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com
> and delete this message and any copies from your computer and network.
> LGC Limited. Registered in England 2991879.
> Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%
> 2Fmailman%2Flistinfo%2Fr-help=ae0ec65b=8af6ace6=y
> PLEASE do read the posting guide https://urlsand.esvalabs.com/?
> u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html=
> ae0ec65b=ea1999c9=y
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread A M Lavezzi
Thanks a lot for your suggestion. I followed the suggestion of Sarah (the
first on the thread) and solved my problem

I will keep into account you suggestion anyway
Mario

On Fri, May 26, 2017 at 3:28 PM, S Ellison  wrote:

> > -Original Message-
> > From: A M > Lavezzi
> >
> > I have data on bilateral trade flows among countries in the following
> form:
> >
> >   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
> >
> >...
> >
> > 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
> >
> > Is there a way to avoid these loops?
>
> Using core R:
> #Use aggregate() to aggregate across years:
>
> https://urlsand.esvalabs.com/?u=http%3A%2F%2FdataTrade.ag=
> ae0ec65b=cb58f304=y <- aggregate (dataTrade[,'Flow',drop=FALSE],
> by=dataTrade[, c('iso_o', 'iso_d')], FUN=sum, na.rm=TRUE)
>
> #where na.rm=TRUE (passed to sum()) essentially treats NAs as 0. If you
> really want NA leave it out or set it to FALSE
> #This gives you one row per origin/destination pair that contains the
> total trade in Flow.
> #If the years you want are a subset, subset the data frame first.
>
> #Form an empty matrix with suitable dimnames:
> N_iso <- length(my_iso)
> dT.m <- matrix(rep(NA, N_iso*N_iso), ncol=N_iso, dimnames=list(my_iso,
> my_iso))
>
> #Then use matrix indexing by name to populate your matrix with the
> available flow data
> dT.m[as.matrix(dataTrade.ag[1:2]) ] <- dataTrade.ag$Flow
> #This relies on a default conversion from data frame factors to a
> character matrix, together
> #with R's facility for matrix indexing by 2-column matrix
>
> #Then
> dataTrade.ag[1:10, 1:10]
>
> #should have what you seem to want
>
>
> S Ellison
>
>
>
>
> ***
> This email and any attachments are confidential. Any u...{{dropped:26}}

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread A M Lavezzi
Hi Duncan
thanks a lot for your suggestion. I followed the suggestion of Sarah (the
first on the thread) and solved my problem

I will keep into account you suggestion anyway
Mario

On Fri, May 26, 2017 at 2:20 PM, Duncan Murdoch 
wrote:

> On 26/05/2017 7:46 AM, A M Lavezzi 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?
>>
>
> Assuming that you have unique entries for each of the first 3 columns, you
> could so something like this:
>
> # Put all the data into an array, indexed by origin, destination, year:
>
> dataMatrix <- as.matrix(dataTrade)  # Converts everything to character
>
> dataArray <- array(0, c(215, 215, 31))
> dimnames(dataArray) <- list(unique(dataMatrix[,1]),
> unique(dataMatrix[,2]), unique(dataMatrix[,3]))
>
> dataArray[dataMatrix[,1:3]] <- dataTrade$FLOW
>
> # Sum across years
>
> apply(dataArray, 3, sum)
>
> I haven't tried this (you didn't give a reproducible example...), so you
> may need to tweak it a bit.
>
> Duncan Murdoch
>



-- 
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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread A M Lavezzi
Hi Ulrik
thanks a lot for your suggestion. I followed the suggestion of Sarah (the
first on the thread) and solved my problem

I will keep into account you suggestion anyway
Mario

On Fri, May 26, 2017 at 2:17 PM, Ulrik Stervbo 
wrote:

> Hi Mario,
>
> does acast from the reshape2 package help?
>
> dfa<- data.frame(iso_o = letters[c(1, 1:4)], iso_d = letters[6:10], year =
> c(1985, 1985, 1986, 1987, 1988), flow = c(1,2,3,4, NA))
> reshape2::acast(dfa, iso_o ~ iso_d, fun.aggregate = sum, value.var =
> "flow")
>
> HTH
> Ulrik
>
> On Fri, 26 May 2017 at 13:47 A M Lavezzi  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
>> 
>> web: http://www.unipa.it/~mario.lavezzi
>> 
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
>


-- 
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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread A M Lavezzi
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 
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 
> 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
>> 
>> web: http://www.unipa.it/~mario.lavezzi
>> 
>>
>> [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> --
> Sarah Goslee
> http://www.stringpage.com
> 
> http://www.sarahgoslee.com
> 
> http://www.functionaldiversity.org
> 
>



-- 
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]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread David L Carlson
How about?

Trade <- xtabs(FLOW ~ iso_o + iso_d + year, dta)

Gives you a 3d table with FLOW as the cell entry. Then

apply(Trade, 1:2, sum, na.rm=TRUE)

Gives you a 2d table with the total flow


David L. Carlson
Department of Anthropology
Texas A University

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of S Ellison
Sent: Friday, May 26, 2017 8:28 AM
To: A M Lavezzi <mario.lave...@unipa.it>; r-help <r-help@r-project.org>
Subject: Re: [R] organizing data in a matrix avoiding loop

> -Original Message-
> From: A M > Lavezzi
> 
> I have data on bilateral trade flows among countries in the following form:
> 
>   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
> 
>...
>
> 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 
> 
> Is there a way to avoid these loops?

Using core R:
#Use aggregate() to aggregate across years:

dataTrade.ag <- aggregate (dataTrade[,'Flow',drop=FALSE], by=dataTrade[, 
c('iso_o', 'iso_d')], FUN=sum, na.rm=TRUE)

#where na.rm=TRUE (passed to sum()) essentially treats NAs as 0. If you really 
want NA leave it out or set it to FALSE
#This gives you one row per origin/destination pair that contains the total 
trade in Flow.
#If the years you want are a subset, subset the data frame first.

#Form an empty matrix with suitable dimnames:
N_iso <- length(my_iso)
dT.m <- matrix(rep(NA, N_iso*N_iso), ncol=N_iso, dimnames=list(my_iso, my_iso))

#Then use matrix indexing by name to populate your matrix with the available 
flow data
dT.m[as.matrix(dataTrade.ag[1:2]) ] <- dataTrade.ag$Flow
#This relies on a default conversion from data frame factors to a 
character matrix, together
#with R's facility for matrix indexing by 2-column matrix

#Then
dataTrade.ag[1:10, 1:10]

#should have what you seem to want


S Ellison




***
This email and any attachments are confidential. Any use...{{dropped:14}}

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread S Ellison
> -Original Message-
> From: A M > Lavezzi
> 
> I have data on bilateral trade flows among countries in the following form:
> 
>   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
> 
>...
>
> 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 
> 
> Is there a way to avoid these loops?

Using core R:
#Use aggregate() to aggregate across years:

dataTrade.ag <- aggregate (dataTrade[,'Flow',drop=FALSE], by=dataTrade[, 
c('iso_o', 'iso_d')], FUN=sum, na.rm=TRUE)

#where na.rm=TRUE (passed to sum()) essentially treats NAs as 0. If you really 
want NA leave it out or set it to FALSE
#This gives you one row per origin/destination pair that contains the total 
trade in Flow.
#If the years you want are a subset, subset the data frame first.

#Form an empty matrix with suitable dimnames:
N_iso <- length(my_iso)
dT.m <- matrix(rep(NA, N_iso*N_iso), ncol=N_iso, dimnames=list(my_iso, my_iso))

#Then use matrix indexing by name to populate your matrix with the available 
flow data
dT.m[as.matrix(dataTrade.ag[1:2]) ] <- dataTrade.ag$Flow
#This relies on a default conversion from data frame factors to a 
character matrix, together
#with R's facility for matrix indexing by 2-column matrix

#Then
dataTrade.ag[1:10, 1:10]

#should have what you seem to want


S Ellison




***
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If 
you have received this message in error, please notify the sender 
immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com 
and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread Duncan Murdoch

On 26/05/2017 7:46 AM, A M Lavezzi 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?


Assuming that you have unique entries for each of the first 3 columns, 
you could so something like this:


# Put all the data into an array, indexed by origin, destination, year:

dataMatrix <- as.matrix(dataTrade)  # Converts everything to character

dataArray <- array(0, c(215, 215, 31))
dimnames(dataArray) <- list(unique(dataMatrix[,1]), 
unique(dataMatrix[,2]), unique(dataMatrix[,3]))


dataArray[dataMatrix[,1:3]] <- dataTrade$FLOW

# Sum across years

apply(dataArray, 3, sum)

I haven't tried this (you didn't give a reproducible example...), so you 
may need to tweak it a bit.


Duncan Murdoch

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread Ulrik Stervbo
Hi Mario,

does acast from the reshape2 package help?

dfa<- data.frame(iso_o = letters[c(1, 1:4)], iso_d = letters[6:10], year =
c(1985, 1985, 1986, 1987, 1988), flow = c(1,2,3,4, NA))
reshape2::acast(dfa, iso_o ~ iso_d, fun.aggregate = sum, value.var = "flow")

HTH
Ulrik

On Fri, 26 May 2017 at 13:47 A M Lavezzi  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
> web: http://www.unipa.it/~mario.lavezzi
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Re: [R] organizing data in a matrix avoiding loop

2017-05-26 Thread Sarah Goslee
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  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
> fax ++39 091 6111268
> skype: lavezzimario
> email: mario.lavezzi (at) unipa.it
> web: http://www.unipa.it/~mario.lavezzi
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.