[R] If cycle takes to much time...

Gerrit Eichner Gerrit.Eichner at math.uni-giessen.de
Fri Jan 25 11:20:44 CET 2013


Hello, Marco,

I am not quite sure if understand correctly what you want, but maybe


DF <- data.frame( Artist, Begin, End, Istitution)
AtSameInst <- outer( DF$Istitution, DF$Istitution, "==")
Simultaneously <- with( DF, outer( Begin, End, "<=") |
                             outer( End, Begin, "<="))
AtSameInst & Simultaneously


gives what you want. (Note that redundant information is computed since 
all resulting matrices are symmetric. The algorithm could certainly be 
optimized.)

  Hth  --  Gerrit

On Fri, 25 Jan 2013, marcoguerzoni wrote:

> dear all,
>
> thank you for reading.
>
> I have a dataset of artists and where and when they had an exhibition.
> I'd like to create an affiliation network in the form of matrix, telling me
> which aritist have been in the same at the same time.
> I manage to do it, but given that I have 96000 observation the program takes
> 30 months to complete.
> her what i have done.
> the data look like this
>
> Artist <-c(1,2,3,2,4,4,5)
> Begin <- as.Date(c('2006-08-23', '2006-03-21', '2006-03-06', '2006-01-13',
> '2006-05-20', '2006-07-13', '2006-07-20'))
> End <- as.Date(c('2006-10-23', '2006-11-30', '2006-05-06', '2006-12-13',
> '2006-09-20', '2006-08-13', '2006-09-20'))
> Istitution <- c(1, 2, 2, 1, 1, 2, 1)
>
> artist is the name of the artist, Begin and End is the when and Istitutionis
> the where.
>
> my IF is working,
>
>
> #number of unique artist
> c <- unique(Artist)
> d <- length(c)
> a <-length(Artist)
>
> B <- mat.or.vec(d,d)
>
> for(i in 1:d) {
> for(j in 1:d) {
> if (Istitution[i]  == Istitution[j]) {
>
>
> if (Begin[i] <= End[j])
> {
> if (End[i]-Begin[j] >= 0) {
> B[i,j] <- B[i,j]+1
> B[i,i] <- 0
>
> }
>
>
> }
> else{
> if (End[j]-Begin[i] >= 0) {
> B[i,j] <- B[i,j]+1
> B[i,i] <- 0
>
>
> }
> }
>  }
>   }
> print(i)
>    }
>
>
> do you have a way to make the programm simpler and faster?
>
> thank you very much
>
> Marco Guerzoni,
> Department of Economics
> University of Turin
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/If-cycle-takes-to-much-time-tp4656601.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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