[R] assign object with loop (translation from SAS to R)

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Jun 29 17:10:53 CEST 2012


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 op 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 op r-project.org [mailto:r-help-bounces op r-project.org] Namens David Winsemius
Verzonden: vrijdag 29 juni 2012 16:18
Aan: lynx
CC: r-help op 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 op 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.



More information about the R-help mailing list