[R] Replacing double loop by apply
Dear R-users, after trying and searching a long time i have the following question. is it possible to replace to following double loop by some apply calls? ### m1 - data.frame(v1=factor(letters[1:5]), v2=factor(letters[2:6]), v3=factor(letters[3:7])) m2 - data.frame(v1=factor(letters[3:7]), v2=factor(letters[1:5]), v3=factor(letters[4:8])) ind - matrix(logical(nrow(m2) * ncol(m2)), nrow = nrow(m2), byrow = TRUE) for (j in 1:ncol(m2)) { for (i in 1:nrow(m2)) { ind[i, j] - any(levels(m1[, j]) == m2[i, j]) } ind[is.na(ind[, j]), j] - TRUE m2[!ind[, j], j] - NA } ### thanks and best regards Andreas -- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! __ 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] Replacing double loop by apply
Try this: mapply(function(x, y)x[match(x, y)], m2, m1) On Fri, May 14, 2010 at 9:23 AM, Andreas Wittmann andreas_wittm...@gmx.dewrote: Dear R-users, after trying and searching a long time i have the following question. is it possible to replace to following double loop by some apply calls? ### m1 - data.frame(v1=factor(letters[1:5]), v2=factor(letters[2:6]), v3=factor(letters[3:7])) m2 - data.frame(v1=factor(letters[3:7]), v2=factor(letters[1:5]), v3=factor(letters[4:8])) ind - matrix(logical(nrow(m2) * ncol(m2)), nrow = nrow(m2), byrow = TRUE) for (j in 1:ncol(m2)) { for (i in 1:nrow(m2)) { ind[i, j] - any(levels(m1[, j]) == m2[i, j]) } ind[is.na(ind[, j]), j] - TRUE m2[!ind[, j], j] - NA } ### thanks and best regards Andreas -- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! __ 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 [[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.
Re: [R] Replacing double loop by apply
Dear Henrique Dallazuanna, thank you very much, this really helped me. by the way, do you thinks it is also possible to do the following loop in R? thanks and best regards Andreas v1 - rnorm(10) v2 - v1+rnorm(10) v3 - v2+rnorm(10) v4 - rnorm(10) y - ifelse(v10,1,0) dat - data.frame(v1=v1,v2=v2,v3=v3,v4=v4) newdat-dat[9:10,] dat - dat[1:8,] newdat[2,1] - NA pred - numeric(2) vars - c(v1,v3,v4) y - y[1:8] for (i in 1:nrow(newdat)) { ind - which(!is.na(newdat[i, ]) colnames(newdat) %in% vars) vars.dat - colnames(dat[ind]) formu - as.formula(paste(cbind(y, 1-y) ~ , paste(vars.dat, collapse = +))) glm_tmp - glm(formu, family = binomial(link = logit), data = dat[, ind]) pred[i] - predict(glm_tmp, newdat = newdat[i, ind], type = response) } On 14.05.2010 14:42, Henrique Dallazuanna wrote: Try this: mapply(function(x, y)x[match(x, y)], m2, m1) On Fri, May 14, 2010 at 9:23 AM, Andreas Wittmann andreas_wittm...@gmx.de mailto:andreas_wittm...@gmx.de wrote: Dear R-users, after trying and searching a long time i have the following question. is it possible to replace to following double loop by some apply calls? ### m1 - data.frame(v1=factor(letters[1:5]), v2=factor(letters[2:6]), v3=factor(letters[3:7])) m2 - data.frame(v1=factor(letters[3:7]), v2=factor(letters[1:5]), v3=factor(letters[4:8])) ind - matrix(logical(nrow(m2) * ncol(m2)), nrow = nrow(m2), byrow = TRUE) for (j in 1:ncol(m2)) { for (i in 1:nrow(m2)) { ind[i, j] - any(levels(m1[, j]) == m2[i, j]) } ind[is.na http://is.na(ind[, j]), j] - TRUE m2[!ind[, j], j] - NA } ### thanks and best regards Andreas -- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! __ R-help@r-project.org mailto: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 [[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.