[R] removing loops from code in making data.frame
arun
smartpink111 at yahoo.com
Tue Jan 15 21:38:12 CET 2013
Hi,
May be this helps:
res<-do.call(rbind,lapply(xaulist,function(x) as.integer(apply(tata,1,function(i) any(i==x)))))
res[]<-sapply(res,as.numeric)
identical(res,tutu)
#[1] TRUE
A.K.
----- Original Message -----
From: Biau David <djmbiau at yahoo.fr>
To: r help list <r-help at 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 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