[R] Avoiding loops in creating a coinvestment matrix
Daniel Malter
daniel at umd.edu
Mon Apr 4 00:16:04 CEST 2011
Hi, I am working on a dataset in which a number of venture capitalists invest
in a number of firms. What I am creating is an asymmetric matrix M in which
m(ij) is the volume (sum) of coinvestments of VC i with VC j (i.e., how much
has VC i invested in companies that VC j also has investments in). The
output should look like the "coinvestments" matrix produced with the code
below. If possible I would like to avoid loops and optimize the code for
speed because the real data is huge. If anybody has suggestions, I would be
grateful.
invest=c(20,50,40,30,10,20,20,30,40)
vc=rep(c('A','B','C'),each=3)
company=c('E','F','G','F','G','H','G','H','I')
data=data.frame(vc,company,invest)
data #data
inv.mat=tapply(invest,list(vc,company),sum)
inv.mat=replace(inv.mat,which(is.na(inv.mat)==T),0)
inv.mat #investment matrix
exist.mat=inv.mat>0
coinvestments<-matrix(0,nrow=length(unique(vc)),ncol=length(unique(vc)))
for(i in unique(vc)){
for(j in unique(vc)){
i.is=which(unique(vc)==i)
j.is=which(unique(vc)==j)
i.invests=exist.mat[i,]
j.invests=exist.mat[j,]
which.i=which(i.invests==T)
which.j=which(j.invests==T)
i.invests.with.j=which.i[which.i%in%which.j]
coinvestments[i.is,j.is]=sum(inv.mat[i.is,i.invests.with.j])
}
}
coinvestments
system.time(
for(i in unique(vc)){
for(j in unique(vc)){
i.is=which(unique(vc)==i)
j.is=which(unique(vc)==j)
i.invests=exist.mat[i,]
j.invests=exist.mat[j,]
which.i=which(i.invests==T)
which.j=which(j.invests==T)
i.invests.with.j=which.i[which.i%in%which.j]
coinvestments[i.is,j.is]=sum(inv.mat[i.is,i.invests.with.j])
}
}
)
Thanks much,
Daniel
--
View this message in context: http://r.789695.n4.nabble.com/Avoiding-loops-in-creating-a-coinvestment-matrix-tp3424298p3424298.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list