[R] Avoid for-loop in creating data.frames
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 __ 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.
Re: [R] Avoid for-loop in creating data.frames
Hi, Is the following close enough? apply(set2, 2, function(x) x[is.na(x)]) HTH, baptiste 2009/12/10 Andreas Wittmann andreas_wittm...@gmx.de: 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 __ 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. __ 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.
Re: [R] Avoid for-loop in creating data.frames
Hi babtiste, thank you very much for your fast answer. your solution is very good, but i also need the dimnames as in the for-loop for further calculations. best regards Andreas baptiste auguie wrote: Hi, Is the following close enough? apply(set2, 2, function(x) x[is.na(x)]) HTH, baptiste 2009/12/10 Andreas Wittmann andreas_wittm...@gmx.de: 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 __ 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. __ 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.
Re: [R] Avoid for-loop in creating data.frames
Try this: lapply(1:ncol(set2), function(idx)set2[is.na(set2[,idx]),idx, drop = FALSE]) On Thu, Dec 10, 2009 at 2:33 PM, Andreas Wittmann andreas_wittm...@gmx.de wrote: Hi babtiste, thank you very much for your fast answer. your solution is very good, but i also need the dimnames as in the for-loop for further calculations. best regards Andreas baptiste auguie wrote: Hi, Is the following close enough? apply(set2, 2, function(x) x[is.na(x)]) HTH, baptiste 2009/12/10 Andreas Wittmann andreas_wittm...@gmx.de: 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 __ 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. __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ 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.