[R] Avoid for-loop in creating data.frames

Andreas Wittmann andreas_wittmann at gmx.de
Thu Dec 10 17:10:30 CET 2009


Dear R-users,

after several tries with lapply and searching the mailing list, i want 
to ask, wheter and how it is possibly to avoid the for-loop in the 
following piece of code?

set2<-as.data.frame(matrix(rnorm(9),ncol=3))

set2[1,1] <- NA
set2[3,2] <- NA
set2[2,1] <- NA

dimnames(set2)[1] <- list(c("A","B","C"))

r <- !is.na(set2)
imp <- vector("list", ncol(set2))

for (j in 1:dim(set2)[2])
{
  imp[[j]] <- as.data.frame(matrix(NA, nrow = sum(!r[,j]), ncol = 1))
  dimnames(imp[[j]]) <- list(row.names(set2)[r[,j] == FALSE], 1)
}


many thanks and best regards

Andreas




More information about the R-help mailing list