thanks, it goes a lot faster. Just one thing though, when I apply the code to my data, both data.frames end up "differente. Or at least identical(df1, df2) if false
however when i do which(df1!=df2) it says 'integer (0)'. Could that be due to the class of the vectors or some thing of the sort? thanks, David Biau >________________________________ > De : arun <smartpink...@yahoo.com> >À : Biau David <djmb...@yahoo.fr> >Cc : R help <r-help@r-project.org> >Envoyé le : Mardi 15 janvier 2013 21h54 >Objet : Re: [R] removing loops from code in making data.frame > >Hi, > >You could also do this: >res1<-do.call(rbind,lapply(xaulist,function(x) >as.numeric(apply(t(mapply(`==`,tata,x)),2,any)))) >identical(res1,tutu) >#[1] TRUE >A.K. > > > > > >----- Original Message ----- >From: Biau David <djmb...@yahoo.fr> >To: r help list <r-help@r-project.org> >Cc: >Sent: Tuesday, January 15, 2013 2:41 PM >Subject: [R] removing loops from code in making data.frame > >Dear all, > >I am working on an author network and to do so I have to arrange a data.frame >(tutu) crossing author names (rows) per publication number (column). The >participation of the author to a study is indicated by a 1 and 0 otherwise. > >I have a vector (xaulist) of all the names of authors and a data.frame (tata) >with all the publications in row and the authors in columns. I have writen a >loop to obtain my data.frame but it takes a long time when the number of >studies increases. I was looking for a more efficient code. > >Here is a minimal working example (my code is terrible i know...): > >#--------------------------------------------------------------------------------- > >au1 <- c('deb', 'art', 'deb', 'seb', 'deb', 'deb', 'mar', 'mar', 'joy', 'deb') >au2 <- c('art', 'deb', 'soy', 'deb', 'joy', 'ani', 'deb', 'deb', 'nem', 'mar') >au3 <- c('mar', 'lio', 'mil', 'mar', 'ani', 'lul', 'nem', 'art', 'deb', 'tat') > >tata <- data.frame(au1, au2, au3) >xaulist2 <- levels(factor(unlist(tata[,]))) >xaulist <- levels(as.factor(xaulist2)) > >tutu <- matrix(NA, nrow=length(xaulist), ncol=dim(tata)[1]) # row are authors >and col are papers >for (i in 1:length(xaulist)) >{ > for (j in 1:dim(tata)[1]) > { > ifelse('TRUE' %in% as.character(tata[j,]==xaulist[i]), tutu[i,j] <- 1, >tutu[i,j] <- 0) > } >} >tutu[is.na(tutu)] <- 0 > >#--------------------------------------------------------------------------------- > >I am looking at some more efficient way to build 'tutu'. > >Thank you very much, > > >David > > [[alternative HTML version deleted]] > > >______________________________________________ >R-help@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. > > > > [[alternative HTML version deleted]]
______________________________________________ R-help@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.