[R] Beginner Loop Question with dynamic variable names

2006-09-25 Thread Peter Wolkerstorfer - CURE
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] [EMAIL PROTECTED]
[Web]  http://www.cure.at

__
R-help@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.


Re: [R] Beginner Loop Question with dynamic variable names

2006-09-25 Thread David Barron
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
}


On 25/09/06, Peter Wolkerstorfer - CURE [EMAIL PROTECTED] 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] [EMAIL PROTECTED]
 [Web]  http://www.cure.at

 __
 R-help@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@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.


Re: [R] Beginner Loop Question with dynamic variable names

2006-09-25 Thread Dimitris Rizopoulos
- Original Message - 
From: David Barron [EMAIL PROTECTED]
To: Peter Wolkerstorfer - CURE [EMAIL PROTECTED]; r-help 
r-help@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 [EMAIL PROTECTED] 
 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] [EMAIL PROTECTED]
 [Web]  http://www.cure.at

 __
 R-help@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@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

__
R-help@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.


Re: [R] Beginner Loop Question with dynamic variable names

2006-09-25 Thread Mike Nielsen
Is this what you had in mind?

 j-data.frame(q1=rnorm(10),q2=rnorm(10))
 j
   q1 q2
1  -0.9189618 -0.2832102
2   0.9394316  1.1345975
3  -0.6388848  0.6850255
4   0.4938245 -0.5825715
5  -1.2885257 -0.2654023
6  -0.5278295  0.2382791
7   0.6517268  0.8923375
8   0.4124178  1.1231630
9  -0.1604982  0.2285672
10 -0.2369713  0.6130197
 for(i in 1:3){j[,paste(sep=,res,i)]-with(j,q1+q2)}
 j
q1  q2res1res2
res3
1  -0.9189618 -0.2832102 -1.20217207 -1.20217207 -1.20217207
2   0.9394316  1.1345975  2.07402913  2.07402913  2.07402913
3  -0.6388848  0.6850255  0.04614073  0.04614073  0.04614073
4   0.4938245 -0.5825715 -0.08874699 -0.08874699 -0.08874699
5  -1.2885257 -0.2654023 -1.55392802 -1.55392802 -1.55392802
6  -0.5278295  0.2382791 -0.28955044 -0.28955044 -0.28955044
7   0.6517268  0.8923375  1.54406433  1.54406433  1.54406433
8   0.4124178  1.1231630  1.53558084  1.53558084  1.53558084
9  -0.1604982  0.2285672  0.06806901  0.06806901  0.06806901
10 -0.2369713  0.6130197  0.37604847  0.37604847  0.37604847

Regards,

Mike
On 9/25/06, Peter Wolkerstorfer - CURE [EMAIL PROTECTED] 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] [EMAIL PROTECTED]
 [Web]  http://www.cure.at

 __
 R-help@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.



-- 
Regards,

Mike Nielsen

__
R-help@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.


[R] for() loop question

2006-08-26 Thread Wensui Liu
Dear Lister,

If I have a list of number, say x-c(0.1, 0.5, 0.6...), how to use a for()
to loop through each number in x one by one?

Thank you so much!

wensui

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] for() loop question

2006-08-26 Thread Marc Schwartz
On Sat, 2006-08-26 at 13:06 -0400, Wensui Liu wrote:
 Dear Lister,
 
 If I have a list of number, say x-c(0.1, 0.5, 0.6...), how to use a for()
 to loop through each number in x one by one?
 
 Thank you so much!
 
 wensui

Two options:

x - c(0.1, 0.5, 0.6)

 for (i in x) {print (i)}
[1] 0.1
[1] 0.5
[1] 0.6


 for (i in seq(along = x)) {print (x[i])}
[1] 0.1
[1] 0.5
[1] 0.6


Which approach you take tends to depends upon what else you are doing
within the loop.

I would also take a look at ?sapply, depending up what is it you are
doing.

HTH,

Marc Schwartz

__
R-help@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.


Re: [R] for() loop question

2006-08-26 Thread MARK LEEDS
let us know what you want to do because the beauty of R is that, in many 
cases, you may not have to loop.


- Original Message - 
From: Wensui Liu [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Saturday, August 26, 2006 1:06 PM
Subject: [R] for() loop question


 Dear Lister,

 If I have a list of number, say x-c(0.1, 0.5, 0.6...), how to use a for()
 to loop through each number in x one by one?

 Thank you so much!

 wensui

 [[alternative HTML version deleted]]

 __
 R-help@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.


__
R-help@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.


[R] a loop question

2004-02-27 Thread Susan Lin
I want to get three .gif image files test.1.gif,
test.2.gif,  test.3.gif by using a loop. The code I
tried  is like this:
  x=c(0, 1, 2, 3, 4)
  y=c(1, 2, 3, 4)
  for(i in 1:3)
  {
x11()
jpeg(test.i.gif)
plot(x, y)
dev.off()

  } 
but I only could get one image file, test.i.gif. How
can I get three image files?

Thanks.

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] a loop question

2004-02-27 Thread Achim Zeileis
On Fri, 27 Feb 2004 13:09:40 -0800 (PST) Susan Lin wrote:

 I want to get three .gif image files test.1.gif,
 test.2.gif,  test.3.gif by using a loop. The code I
 tried  is like this:
   x=c(0, 1, 2, 3, 4)
   y=c(1, 2, 3, 4)
   for(i in 1:3)
   {
 x11()
 jpeg(test.i.gif)

This should be

jpeg(paste(test., i, .gif, sep = ))

also look at help(paste).

hth,
Z

 plot(x, y)
 dev.off()
 
   } 
 but I only could get one image file, test.i.gif. How
 can I get three image files?
 
 Thanks.
 
 __
 [EMAIL PROTECTED] mailing list
 https://www.stat.math.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide!
 http://www.R-project.org/posting-guide.html


__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] a loop question

2004-02-27 Thread Ko-Kang Kevin Wang

 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Susan Lin
 Sent: Saturday, February 28, 2004 10:10 AM
 To: [EMAIL PROTECTED]
 Subject: [R] a loop question

 I want to get three .gif image files test.1.gif,
 test.2.gif,  test.3.gif by using a loop. The code I
 tried  is like this:
   x=c(0, 1, 2, 3, 4)
   y=c(1, 2, 3, 4)
   for(i in 1:3)
   {
 x11()
 jpeg(test.i.gif)
 plot(x, y)
 dev.off()

   }
 but I only could get one image file, test.i.gif. How
 can I get three image files?

How about (I haven't tried it) something like:
  jpeg(paste(test., i, .gif, sep = )


Ko-Kang Kevin Wang, MSc(Hon)
SLC Stats Workshops Co-ordinator
The University of Auckland
New Zealand



Ko-Kang Kevin Wang, MSc(Hon)
SLC Stats Workshops Co-ordinator
The University of Auckland
New Zealand

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html