[R] Beginner Loop Question with dynamic variable names

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Mon Sep 25 15:55:01 CEST 2006


----- Original Message ----- 
From: "David Barron" <mothsailor at googlemail.com>
To: "Peter Wolkerstorfer - CURE" <wolkerstorfer at cure.at>; "r-help" 
<r-help at stat.math.ethz.ch>
Sent: Monday, September 25, 2006 3:33 PM
Subject: Re: [R] Beginner Loop Question with dynamic variable names


>I think this does what you are looking for:
>
> dta <- 
> data.frame(q1_1=rep(1,5),q1_2=rep(2,5),q2_1=rep(3,5),q2_2=rep(4,5))
>
> for (i in 1:2) {
>    e1  <- paste("q",i,"_1 + q",i,"_2 * 2",sep="")
>    assign(paste("q",i,sep=""),with(dta,eval(parse(text=e1))))
> }
>

or something like the following if you want to avoid eval(parse(text = 
...)):

dta <- data.frame(q1_1 = rep(1,5), q1_2 = rep(2,5), q1_3 = rep(1,5), 
q1_4 = rep(2,5),
                  q2_1 = rep(3,5), q2_2 = rep(4,5), q2_3 = rep(3,5), 
q2_4 = rep(4,5),
                  q3_1 = rep(3,5), q3_2 = rep(4,5), q3_3 = rep(3,5), 
q3_4 = rep(4,5))

for (i in 1:3) {
    nam <- paste("q", i, sep = "")
    e1  <- data.matrix(dta[grep(nam, names(dta), fixed = TRUE)])
    dta <- cbind(dta, rowSums(e1 * rep(1:ncol(e1), each = nrow(e1))))
    names(dta)[length(dta)] <- nam
}

dta


Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


> On 25/09/06, Peter Wolkerstorfer - CURE <wolkerstorfer at cure.at> 
> wrote:
>> Dear all,
>>
>> I have another small scripting-beginner problem which you hopefully 
>> can
>> help:
>>
>> I compute new variables with:
>>
>> # Question 1
>> results$q1 <- with(results, q1_1*1+ q1_2*2+ q1_3*3+ q1_4*4+ q1_5*5)
>> # Question 2
>> results$q2 <- with(results, q2_1*1+ q2_2*2+ q2_3*3+ q2_4*4+ q2_5*5)
>> # Question 3
>> results$q3 <- with(results, q3_1*1+ q3_2*2+ q3_3*3+ q3_4*4+ q3_5*5)
>> # Question 4
>> results$q4 <- with(results, q4_1*1+ q4_2*2+ q4_3*3+ q4_4*4+ q4_5*5)
>>
>> This is very inefficient so I would like to do this in a loop like:
>>
>> for (i in 1:20) {results$q1 <- with(results, q1_1*1+ q1_2*2+ 
>> q1_3*3+
>> q1_4*4+ q1_5*5)}
>>
>> My question now:
>> How to replace the "1"-s (results$q1, q1_1...) in the variables 
>> with the
>> looping variable?
>>
>> Here like I like it (just for illustration - of course I still miss 
>> the
>> function to tell R that it should append the value of i to the 
>> variable
>> name):
>>
>> # i is the number of questions - just an illustration, I know it 
>> does
>> not work this way
>> for (i in 1:20) {results$qi <- with(results, qi_1*1+ qi_2*2+ 
>> qi_3*3+
>> qi_4*4+ qi_5*5)}
>>
>> Help would be greatly appreciated. Thanks in advance.
>>
>> Peter
>>
>>
>> ___CURE - Center for Usability Research & Engineering___
>>
>> Peter Wolkerstorfer
>> Usability Engineer
>> Hauffgasse 3-5, 1110 Wien, Austria
>>
>> [Tel]  +43.1.743 54 51.46
>> [Fax]  +43.1.743 54 51.30
>>
>> [Mail] wolkerstorfer at cure.at
>> [Web]  http://www.cure.at
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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 Barron
> Said Business School
> University of Oxford
> Park End Street
> Oxford OX1 1HP
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list