Skip to content

assign object with loop (translation from SAS to R)

6 messages · Rui Barradas, David Winsemius, ONKELINX, Thierry +2 more

#
Hello,

See inline.

Em 29-06-2012 02:18, lynx escreveu:
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
#
On Jun 28, 2012, at 9:18 PM, lynx wrote:

            
# Try instead:

DM[[ paste("p",i, "p",j,sep="") ]] <-
           DM[[paste("p",i, sep="")]] * DM[[paste("p",i, sep="")]]
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)
#
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:

            
# Try instead:

DM[[ paste("p",i, "p",j,sep="") ]] <-
           DM[[paste("p",i, sep="")]] * DM[[paste("p",i, sep="")]]
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.
#
With R it is pretty easy to eliminate the loops and the use of outer() 
(which calculates each product twice) using expand.grid():
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
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.