I have a dataset named DM with p1, p2, ...., p9 (9 columns, numerical values) I would like to calculate to multify each pair of columns (p1p2, p1p3,... p1p9, p2p3, p2p4.... p8p9) and assign them in p1p2, p1p3,... p1p9, p2p3, p2p4.... p8p9 In SAS, l=0; p_int_sum=0; do i=1 to 8; do j=(i+1) to 9; l=l+1; p{i}p{j}=p{i}*p{j}; end; end; I would like to know how to assign them in R I tried for function but failed. for (i in 1:8) { for (j in 2:9) { DM$p[i]p[j] <- DM$p[i] * DM$p[j] }} Thank you so much for reading this! -- View this message in context: http://r.789695.n4.nabble.com/assign-object-with-loop-translation-from-SAS-to-R-tp4634806.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Rui Barradas
2012-Jun-29 08:02 UTC
[R] assign object with loop (translation from SAS to R)
Hello, See inline. Em 29-06-2012 02:18, lynx escreveu:> I have a dataset named DM with p1, p2, ...., p9 (9 columns, numerical values) > I would like to calculate to multify each pair of columns (p1p2, p1p3,... > p1p9, p2p3, p2p4.... p8p9) and assign them in p1p2, p1p3,... p1p9, p2p3, > p2p4.... p8p9 > > In SAS, > > l=0; > p_int_sum=0; > do i=1 to 8; > do j=(i+1) to 9; > l=l+1; > p{i}p{j}=p{i}*p{j}; > end; > end; >In R this sort of syntax doesn't work, the trick is to create the variable where to hold the rsult beforehand and use vectors. DM <- data.frame(p=1:9) DM$prod <- NA # create results variable i <- 2:9 # use a vector to index DM$p DM$prod[i] <- DM$p[i - 1]*DM$p[i] DM Hope this helps, Rui Barradas> I would like to know how to assign them in R > I tried for function but failed. > for (i in 1:8) { > for (j in 2:9) { > DM$p[i]p[j] <- DM$p[i] * DM$p[j] > > }} > > Thank you so much for reading this! > > -- > View this message in context: http://r.789695.n4.nabble.com/assign-object-with-loop-translation-from-SAS-to-R-tp4634806.html > Sent from the R help mailing list archive at Nabble.com. > [[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. >
David Winsemius
2012-Jun-29 14:18 UTC
[R] assign object with loop (translation from SAS to R)
On Jun 28, 2012, at 9:18 PM, lynx wrote:> I have a dataset named DM with p1, p2, ...., p9 (9 columns, > numerical values) > I would like to calculate to multify each pair of columns (p1p2, > p1p3,... > p1p9, p2p3, p2p4.... p8p9) and assign them in p1p2, p1p3,... p1p9, > p2p3, > p2p4.... p8p9 > > In SAS, > > l=0; > p_int_sum=0; > do i=1 to 8; > do j=(i+1) to 9; > l=l+1; > p{i}p{j}=p{i}*p{j}; > end; > end; > > I would like to know how to assign them in R > I tried for function but failed. > for (i in 1:8) { > for (j in 2:9) {# Try instead: DM[[ paste("p",i, "p",j,sep="") ]] <- DM[[paste("p",i, sep="")]] * DM[[paste("p",i, sep="")]]> DM$p[i]p[j] <- DM$p[i] * DM$p[j] > > }}I suspect there is a more elegant method than this use of R as a macro processor. I tested the above approach with suitably smalled subscripts on a smaller dataset: DM <- data.frame(p1=1:10,p2=1:10,p3=1:10,p4=1:10,p5=1:10) -- David Winsemius, MD West Hartford, CT
ONKELINX, Thierry
2012-Jun-29 15:10 UTC
[R] assign object with loop (translation from SAS to R)
You can use a combination of the outer() and apply() functions n <- 10 p <- 9 dataset <- data.frame(matrix(rep(seq_len(p), each = n), nrow = n, ncol = p)) colnames(dataset) <- paste("p", seq_len(p), sep = "") test <- t(apply(dataset, 1, function(x){ x %o% x})) colnames(test) <- paste("p", rep(seq_len(p), each = p), "p", rep(seq_len(p), p), sep = "") test <- test[, rep(seq_len(p), each = p) < rep(seq_len(p), p)] ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium + 32 2 525 02 51 + 32 54 43 61 85 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens David Winsemius Verzonden: vrijdag 29 juni 2012 16:18 Aan: lynx CC: r-help at r-project.org Onderwerp: Re: [R] assign object with loop (translation from SAS to R) On Jun 28, 2012, at 9:18 PM, lynx wrote:> I have a dataset named DM with p1, p2, ...., p9 (9 columns, numerical > values) I would like to calculate to multify each pair of columns > (p1p2, p1p3,... > p1p9, p2p3, p2p4.... p8p9) and assign them in p1p2, p1p3,... p1p9, > p2p3, p2p4.... p8p9 > > In SAS, > > l=0; > p_int_sum=0; > do i=1 to 8; > do j=(i+1) to 9; > l=l+1; > p{i}p{j}=p{i}*p{j}; > end; > end; > > I would like to know how to assign them in R I tried for function but > failed. > for (i in 1:8) { > for (j in 2:9) {# Try instead: DM[[ paste("p",i, "p",j,sep="") ]] <- DM[[paste("p",i, sep="")]] * DM[[paste("p",i, sep="")]]> DM$p[i]p[j] <- DM$p[i] * DM$p[j] > > }}I suspect there is a more elegant method than this use of R as a macro processor. I tested the above approach with suitably smalled subscripts on a smaller dataset: DM <- data.frame(p1=1:10,p2=1:10,p3=1:10,p4=1:10,p5=1:10) -- David Winsemius, MD West Hartford, CT ______________________________________________ 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. * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * * Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
David L Carlson
2012-Jun-29 16:54 UTC
[R] assign object with loop (translation from SAS to R)
With R it is pretty easy to eliminate the loops and the use of outer() (which calculates each product twice) using expand.grid():> # Create some data and label columns > DM <- data.frame(matrix(round(runif(90)*10, 0), ncol=9)) > colnames(DM) <- paste0("p", 1:9) > > # Create i, j indices and label columns > idx <- expand.grid(2:9, 1:8) > colnames(idx) <- c("j", "i") > > # Compute products in DM2 and label columns > DM2 <- DM[idx$i]*DM[idx$j] > colnames(DM2) <- paste0("p", idx$i, "p", idx$j) > > # Combine original data with products > DM <- data.frame(DM, DM2)---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of ONKELINX, Thierry > Sent: Friday, June 29, 2012 10:11 AM > To: David Winsemius; lynx > Cc: r-help at r-project.org > Subject: Re: [R] assign object with loop (translation from SAS to R) > > You can use a combination of the outer() and apply() functions > > n <- 10 > p <- 9 > dataset <- data.frame(matrix(rep(seq_len(p), each = n), nrow = n, ncol > = p)) > colnames(dataset) <- paste("p", seq_len(p), sep = "") > test <- t(apply(dataset, 1, function(x){ x %o% x})) > colnames(test) <- paste("p", rep(seq_len(p), each = p), "p", > rep(seq_len(p), p), sep = "") > test <- test[, rep(seq_len(p), each = p) < rep(seq_len(p), p)] > > > > ir. Thierry Onkelinx > Instituut voor natuur- en bosonderzoek / Research Institute for Nature > and Forest > team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance > Kliniekstraat 25 > 1070 Anderlecht > Belgium > + 32 2 525 02 51 > + 32 54 43 61 85 > Thierry.Onkelinx at inbo.be > www.inbo.be > > To call in the statistician after the experiment is done may be no more > than asking him to perform a post-mortem examination: he may be able to > say what the experiment died of. > ~ Sir Ronald Aylmer Fisher > > The plural of anecdote is not data. > ~ Roger Brinner > > The combination of some data and an aching desire for an answer does > not ensure that a reasonable answer can be extracted from a given body > of data. > ~ John Tukey > > -----Oorspronkelijk bericht----- > Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > Namens David Winsemius > Verzonden: vrijdag 29 juni 2012 16:18 > Aan: lynx > CC: r-help at r-project.org > Onderwerp: Re: [R] assign object with loop (translation from SAS to R) > > > On Jun 28, 2012, at 9:18 PM, lynx wrote: > > > I have a dataset named DM with p1, p2, ...., p9 (9 columns, numerical > > values) I would like to calculate to multify each pair of columns > > (p1p2, p1p3,... > > p1p9, p2p3, p2p4.... p8p9) and assign them in p1p2, p1p3,... p1p9, > > p2p3, p2p4.... p8p9 > > > > In SAS, > > > > l=0; > > p_int_sum=0; > > do i=1 to 8; > > do j=(i+1) to 9; > > l=l+1; > > p{i}p{j}=p{i}*p{j}; > > end; > > end; > > > > I would like to know how to assign them in R I tried for function but > > failed. > > for (i in 1:8) { > > for (j in 2:9) { > # Try instead: > > DM[[ paste("p",i, "p",j,sep="") ]] <- > DM[[paste("p",i, sep="")]] * DM[[paste("p",i, sep="")]] > > > DM$p[i]p[j] <- DM$p[i] * DM$p[j] > > > > }} > > I suspect there is a more elegant method than this use of R as a macro > processor. I tested the above approach with suitably smalled subscripts > on a smaller dataset: > > DM <- data.frame(p1=1:10,p2=1:10,p3=1:10,p4=1:10,p5=1:10) > > > -- > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > 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. > * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * * > Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver > weer en binden het INBO onder geen enkel beding, zolang dit bericht > niet bevestigd is door een geldig ondertekend document. > The views expressed in this message and any annex are purely those of > the writer and may not be regarded as stating an official position of > INBO, as long as the message is not confirmed by a duly signed > document. > > ______________________________________________ > 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.
Dear Rui Barradas David Winsemius ONKELINX, Thierry David Carlson I really appreciate your helps. I did not realize that there were such many ways to do it. Among them, I just pick up the simple one and it worked out. It was like this. <<ONKELINX, Thierry's way>> n <- 10 p <- 9 dataset <- data.frame(matrix(rep(seq_len(p), each = n), nrow = n, ncol = p)) colnames(dataset) <- paste("p", seq_len(p), sep = "") test <- t(apply(dataset, 1, function(x){ x %o% x})) colnames(test) <- paste("p", rep(seq_len(p), each = p), "p", rep(seq_len(p), p), sep = "") test <- test[, rep(seq_len(p), each = p) < rep(seq_len(p), p)] In fact, this is first time for me to post in R help. I was very surprised to see quick replys and how you are supportive. You guys are awesome. Thank you so so much! I will pay back the debt to someone else. :-) -- View this message in context: http://r.789695.n4.nabble.com/assign-object-with-loop-translation-from-SAS-to-R-tp4634806p4634941.html Sent from the R help mailing list archive at Nabble.com.