[R] assign object with loop (translation from SAS to R)
David L Carlson
dcarlson at tamu.edu
Fri Jun 29 18:54:05 CEST 2012
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.
More information about the R-help
mailing list