Re: [R] Loop question?

2013-01-26 Thread David Hugh-Jones
Here's a toy example which you can apply the logic of:

dfr - expand.grid(1:3,1:2)
results - apply(dfr, 1, sum)
dfr[results==4,]





On 25 January 2013 22:19, Andras Farkas motyoc...@yahoo.com wrote:

 Dear All

 I have the following data (somewhat simplyfied):

 TINF -1
 a -c(500,750,1000,1250,1500,1750,2000)
 b -c(8,12,18,24,36,48,60,72,96)

 following function:

 infcprodessa -function (D, tin, tau, ts)
   (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1 - exp(-0.048 * tau 
 * exp(-0.048 * (ts - tin))

 z -sapply(1:1, function(n) infcprodessa(1000,TINF,12,12-TINF))

 is there a way to select the combination of respective a and b values that 
 would result in a calculated z that is between 15 and 20? In this case the a 
 would be 1000 and the b would be 12 (other combinations are also possible), 
 but how could I automatically find them? perhaps a loop?

 Apreciate the help,

 Sincerely,

 Andras
 [[alternative HTML version deleted]]


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


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


Re: [R] Loop question?

2013-01-26 Thread Andras Farkas

Sorry Jeff, probably the new version of Yahoo mail doing the html, I switched 
back to the older one hope that takes care of the problem. Let me clarify the 
code below:

TINF -1
a -c(500,750,1000,1250,1500,1750,2000)
b -c(8,12,18,24,36,48,60,72,96)

infcprodessa -function (D, tin, tau, ts) 
 (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1 - exp(-0.048 * tau 
* exp(-0.048 * (ts - tin))

z -sapply(1:1, function(n) infcprodessa(a,TINF,b,b-TINF)) 

so I am looking to find the combination of a = 1000 and b = 12, which iz a 
value for z that would fall between 15 and 20. Sometimes though there may be 
more than one combinations that will meet criteria, in that case I would like 
to select the combination based on the smallest a value that has the 
respective b value to meet the criteria,

sorry for the confusion,

thanks,

Andras


--- On Sat, 1/26/13, Jeff Newmiller jdnew...@dcn.davis.ca.us wrote:

 From: Jeff Newmiller jdnew...@dcn.davis.ca.us
 Subject: Re: [R] Loop question?
 To: Andras Farkas motyoc...@yahoo.com, r-help@r-project.org 
 r-help@r-project.org
 Date: Saturday, January 26, 2013, 2:09 AM
 Please read the Posting Guide
 no html email
 reproducible example please
 
 In general, you can use expand.grid to generate all
 combinations of inputs, compute results as a vector just as
 long as the expand.grid data frame has rows, and identify
 which results meet your criteria by a logical test, and use
 that test to identify which input combinations worked.
 Provide a working starting point and someone might give you
 working code as an answer. (where do a and b come into your
 problem?)
 ---
 Jeff Newmiller           
             The 
    .   
    .  Go Live...
 DCN:jdnew...@dcn.davis.ca.us 
       Basics: ##.#.   
    ##.#.  Live Go...
                
                
       Live:   OO#.. Dead:
 OO#..  Playing
 Research Engineer (Solar/Batteries     
       O.O#.   
    #.O#.  with
 /Software/Embedded Controllers)       
        .OO#.   
    .OO#.  rocks...1k
 ---
 
 Sent from my phone. Please excuse my brevity.
 
 Andras Farkas motyoc...@yahoo.com
 wrote:
 
 Dear All
 �
 I have the following data (somewhat simplyfied):
 �
 TINF -1
 a -c(500,750,1000,1250,1500,1750,2000)
 b -c(8,12,18,24,36,48,60,72,96)
 �
 following function:
 �
 infcprodessa -function (D, tin, tau, ts) 
 � (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1
 - exp(-0.048 *
 tau * exp(-0.048 * (ts - tin))
 
 z -sapply(1:1, function(n)
 infcprodessa(1000,TINF,12,12-TINF))
 �
 is there a way to select the combination of respective a
 and b values
 that would result in a calculated z that is between 15
 and 20? In this
 case the a would be 1000 and the b would be 12 (other
 combinations are
 also possible), but how could I automatically find them?
 perhaps a
 loop?
 �
 Apreciate the help,
 �
 Sincerely,
 �
 Andras
     [[alternative HTML version deleted]]
 
 
 
 
 
 __
 R-help@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.
 


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


Re: [R] Loop question?

2013-01-26 Thread Berend Hasselman

On 26-01-2013, at 12:31, Andras Farkas motyoc...@yahoo.com wrote:

 
 Sorry Jeff, probably the new version of Yahoo mail doing the html, I switched 
 back to the older one hope that takes care of the problem. Let me clarify the 
 code below:
 
 TINF -1
 a -c(500,750,1000,1250,1500,1750,2000)
 b -c(8,12,18,24,36,48,60,72,96)
 
 infcprodessa -function (D, tin, tau, ts) 
 (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1 - exp(-0.048 * 
 tau * exp(-0.048 * (ts - tin))
 
 z -sapply(1:1, function(n) infcprodessa(a,TINF,b,b-TINF)) 
 
 so I am looking to find the combination of a = 1000 and b = 12, which iz a 
 value for z that would fall between 15 and 20. Sometimes though there may 
 be more than one combinations that will meet criteria, in that case I would 
 like to select the combination based on the smallest a value that has the 
 respective b value to meet the criteria,
 

ab - expand.grid(a=a,b=b)
z - unlist(sapply(seq_len(nrow(ab)), function(k) 
infcprodessa(ab[k,1],TINF,ab[k,2],ab[k,2]-TINF)))

z.target - which(z=15  z=20)
ab[z.target,]
z[z.target]
which.min(ab[z.target,][,a])

Berend

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


Re: [R] Loop question?

2013-01-26 Thread Jeff Newmiller
The unlist-sapply-seq_len bit is unnecessarily convoluted, since the 
infcprodessa function can accept vector inputs.

z - infcprodessa( ab$a, TINF, ab$b, ab$b-TINF )
possibles - ab[ z = 15  z = 20, ]
possibles[ which.min( possibles$a ), ]
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Berend Hasselman b...@xs4all.nl wrote:


On 26-01-2013, at 12:31, Andras Farkas motyoc...@yahoo.com wrote:

 
 Sorry Jeff, probably the new version of Yahoo mail doing the html, I
switched back to the older one hope that takes care of the problem. Let
me clarify the code below:
 
 TINF -1
 a -c(500,750,1000,1250,1500,1750,2000)
 b -c(8,12,18,24,36,48,60,72,96)
 
 infcprodessa -function (D, tin, tau, ts) 
 (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1 - exp(-0.048
* tau * exp(-0.048 * (ts - tin))
 
 z -sapply(1:1, function(n) infcprodessa(a,TINF,b,b-TINF)) 
 
 so I am looking to find the combination of a = 1000 and b = 12, which
iz a value for z that would fall between 15 and 20. Sometimes though
there may be more than one combinations that will meet criteria, in
that case I would like to select the combination based on the smallest
a value that has the respective b value to meet the criteria,
 

ab - expand.grid(a=a,b=b)
z - unlist(sapply(seq_len(nrow(ab)), function(k)
infcprodessa(ab[k,1],TINF,ab[k,2],ab[k,2]-TINF)))

z.target - which(z=15  z=20)
ab[z.target,]
z[z.target]
which.min(ab[z.target,][,a])

Berend

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

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


[R] Loop question?

2013-01-25 Thread Andras Farkas
Dear All
 
I have the following data (somewhat simplyfied):
 
TINF -1
a -c(500,750,1000,1250,1500,1750,2000)
b -c(8,12,18,24,36,48,60,72,96)
 
following function:
 
infcprodessa -function (D, tin, tau, ts) 
  (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1 - exp(-0.048 * tau * 
exp(-0.048 * (ts - tin))

z -sapply(1:1, function(n) infcprodessa(1000,TINF,12,12-TINF))
 
is there a way to select the combination of respective a and b values that 
would result in a calculated z that is between 15 and 20? In this case the a 
would be 1000 and the b would be 12 (other combinations are also possible), but 
how could I automatically find them? perhaps a loop?
 
Apreciate the help,
 
Sincerely,
 
Andras
[[alternative HTML version deleted]]

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


Re: [R] Loop question?

2013-01-25 Thread Jeff Newmiller
Please read the Posting Guide
no html email
reproducible example please

In general, you can use expand.grid to generate all combinations of inputs, 
compute results as a vector just as long as the expand.grid data frame has 
rows, and identify which results meet your criteria by a logical test, and use 
that test to identify which input combinations worked.
Provide a working starting point and someone might give you working code as an 
answer. (where do a and b come into your problem?)
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Andras Farkas motyoc...@yahoo.com wrote:

Dear All
�
I have the following data (somewhat simplyfied):
�
TINF -1
a -c(500,750,1000,1250,1500,1750,2000)
b -c(8,12,18,24,36,48,60,72,96)
�
following function:
�
infcprodessa -function (D, tin, tau, ts) 
� (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1 - exp(-0.048 *
tau * exp(-0.048 * (ts - tin))

z -sapply(1:1, function(n) infcprodessa(1000,TINF,12,12-TINF))
�
is there a way to select the combination of respective a and b values
that would result in a calculated z that is between 15 and 20? In this
case the a would be 1000 and the b would be 12 (other combinations are
also possible), but how could I automatically find them? perhaps a
loop?
�
Apreciate the help,
�
Sincerely,
�
Andras
   [[alternative HTML version deleted]]





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

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


Re: [R] Loop question

2012-05-31 Thread Özgür Asar
Dear Sebastian,

The following will create the names

paste(sb,1:5,sep=)
paste(sw,1:5,sep=)
paste(Lw,1:5,sep=)
paste(Lb,1:5,sep=)

Then you can easily combine and/or order them in R.

Hope this helps.
Ozgur

-

Ozgur ASAR

Research Assistant
Middle East Technical University
Department of Statistics
06531, Ankara Turkey
Ph: 90-312-2105309
http://www.stat.metu.edu.tr/people/assistants/ozgur/
--
View this message in context: 
http://r.789695.n4.nabble.com/Loop-question-tp4631896p4631900.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] Loop question

2012-05-31 Thread R. Michael Weylandt
Note that in R = 2.15 you can also use paste0 for this operation more
efficiently.

Michael

On Thu, May 31, 2012 at 1:58 AM, Özgür Asar oa...@metu.edu.tr wrote:
 Dear Sebastian,

 The following will create the names

 paste(sb,1:5,sep=)
 paste(sw,1:5,sep=)
 paste(Lw,1:5,sep=)
 paste(Lb,1:5,sep=)

 Then you can easily combine and/or order them in R.

 Hope this helps.
 Ozgur

 -
 
 Ozgur ASAR

 Research Assistant
 Middle East Technical University
 Department of Statistics
 06531, Ankara Turkey
 Ph: 90-312-2105309
 http://www.stat.metu.edu.tr/people/assistants/ozgur/
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Loop-question-tp4631896p4631900.html
 Sent from the R help mailing list archive at Nabble.com.

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

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


[R] Loop question

2012-05-30 Thread Sebastián Daza
Hello,
I have a dataframe (Lx) with 5 Lb, and 5 Lw variables. I want to
create several variables according to the loop presented below (data
attached).

Lx - read.csv(Lx.csv, header=T, sep=,)

for (i in 1:20) {
  Lx$sb1[i]  - Lx$Lb1[i+1]/Lx$Lb1[i]
  Lx$sb2[i]  - Lx$Lb2[i+1]/Lx$Lb2[i]
  Lx$sb3[i]  - Lx$Lb3[i+1]/Lx$Lb3[i]
  Lx$sb4[i]  - Lx$Lb4[i+1]/Lx$Lb4[i]
  Lx$sb5[i]  - Lx$Lb5[i+1]/Lx$Lb5[i]
  Lx$sw1[i]  - Lx$Lw1[i+1]/Lx$Lw1[i]
  Lx$sw2[i]  - Lx$Lw2[i+1]/Lx$Lw2[i]
  Lx$sw3[i]  - Lx$Lw3[i+1]/Lx$Lw3[i]
  Lx$sw4[i]  - Lx$Lw4[i+1]/Lx$Lw4[i]
  Lx$sw5[i]  - Lx$Lw5[i+1]/Lx$Lw5[i]
}

How I could also create the variable names (letters b and w, and
numbers from 1 to 5 in s and L variables) using a loop in R?
In Stata I can use this command:

 foreach r in b w {;
   foreach s of numlist 0(5)40 {;
   foreach ed of numlist 1/5 {;
   local f = `s'+5;
   scalar define rL`r'`ed'_`f'over`s' = L`r'`ed'_`f' / L`r'`ed'_`s';
   };
 };
 };

but I do not how to do it in R.
Thank you in advance!

-- 
Sebastián Daza
__
R-help@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.


[R] loop question

2011-04-14 Thread Thomas

Dear all,

I am trying to implement the following in a loop:
g - cbind(c(1, 2, 3), c(1, 2, 3))
h - cbind(c(1, 2, 3), c(1, 2, 3))
c - cbind(g[,1]*h[,1], g[,2]*h[,2])
g-rowSums(c)

My attempt looks like this but does not produce the desired results as 
above.


for (i in 1:2) {g- rowSums(cbind(g[,i]*h[,i]))}

How do I do this correctly?

Many thanks,

Thomas

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


[R] loop question

2011-04-05 Thread Thomas

Dear all,

I am trying to set up a list with 1:c objects each meant to capture the 
coefficients for one coefficient and 100 replications. I receive the 
following error message:


Error in betaboot[[p]] : subscript out of bounds.

My code is below. Where is my mistake?

Many thanks,

Thomas

_
betaboot-list(NULL)

for (i in 1:c) {
betaboot[[i]]-cbind()
}


num - 100 # this is the number of bootstraps

for (i in 1:num) {

[BOOTSTRAP]

  coef.temp - coef(model.temp, data=newdata)

  for (p in 1:c){
  betaboot[[p]] - cbind(betaboot[[p]], coef.temp[,p])
  }

  }

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


Re: [R] loop question

2011-04-05 Thread Joshua Wiley
Dear Thomas,

On Tue, Apr 5, 2011 at 8:33 AM, Thomas thomas.tri...@cantab.net wrote:
 Dear all,

 I am trying to set up a list with 1:c objects each meant to capture the
 coefficients for one coefficient and 100 replications. I receive the
 following error message:

 Error in betaboot[[p]] : subscript out of bounds.

 My code is below. Where is my mistake?

 Many thanks,

 Thomas

 _
 betaboot-list(NULL)

if you know the number of bootstraps (which you seem to later on), a
preferred way to instatiate the list would be:

betaboot - vector(mode = list, length = yourlength)


 for (i in 1:c) {

because c() is such an important function, I would strongly
encourage you not to use it also as a variable.

 betaboot[[i]]-cbind()

Don't use this to build an empty list.

 }


 num - 100 # this is the number of bootstraps

 for (i in 1:num) {

    [BOOTSTRAP]

  coef.temp - coef(model.temp, data=newdata)

  for (p in 1:c){
  betaboot[[p]] - cbind(betaboot[[p]], coef.temp[,p])

This should work assuming betaboot is instatiated properly.  That
said, it looks like you have a nested for loop and then just keep
cbind()ing each element of betaboot bigger and bigger.  You may get a
performance increase if you also instantiate each matrix/dataframe
inside betaboot.  Then the call would become something like:

betaboot[[i]][,p] - coef.temp[,p]

that is, you can use a chained series of extraction operators to get
to the appropriate column in the matrix/dataframe inside the
appropriate list element.  Then rather than constantly using cbind(),
you just place coef.temp[,p] where you want it.  The only requirement
is that you know the sizes of the matrices/dataframes going in so you
can create empty ones from the get go.

Cheers,

Josh

  }

  }

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

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

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


Re: [R] Loop question

2009-04-18 Thread Jun Shen
Brendan,

Matrix is atomic. Once you define t1 in matrix, t1[1]=0  rather than the
whole column. I would just convert t1 to a data frame, which is a special
list, by adding t1- data.frame(t1).  Now t1[1] represents the whole column.
Then you can use your loop to add more columns.

Jun

On Fri, Apr 17, 2009 at 9:12 PM, Brendan Morse morse.bren...@gmail.comwrote:

 Hi everyone, I am trying to accomplish a small task that is giving me quite
 a headache. I would like to automatically generate a series of matrices and
 give them successive names. Here is what I thought at first:

 t1-matrix(0, nrow=250, ncol=1)

 for(i in 1:10){
t1[i]-rnorm(250)
 }

 What I intended was that the loop would create 10 different matrices with a
 single column of 250 values randomly selected from a normal distribution,
 and that they would be labeled t11, t12, t13, t14 etc.

 Can anyone steer me in the right direction with this one?

 Thanks!
 Brendan

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




-- 
Jun Shen PhD
PK/PD Scientist
BioPharma Services
Millipore Corporation
15 Research Park Dr.
St Charles, MO 63304
Direct: 636-720-1589

[[alternative HTML version deleted]]

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


Re: [R] Loop question

2009-04-18 Thread Stavros Macrakis
On Fri, Apr 17, 2009 at 10:12 PM, Brendan Morse morse.bren...@gmail.com wrote:
 ...I would like to automatically generate a series of matrices and
 give them successive names. Here is what I thought at first:

 t1-matrix(0, nrow=250, ncol=1)

 for(i in 1:10){
        t1[i]-rnorm(250)
 }

 What I intended was that the loop would create 10 different matrices with a
 single column of 250 values randomly selected from a normal distribution,
 and that they would be labeled t11, t12, t13, t14 etc.

Very close.  But since you've started out with a *matrix* t1, your
assignments to t1[i] will assign to parts of the matrix.  To correct
this, all you need to do is initialize t1 as a *list of matrices* or
(even better) as an *empty list*, like this:

   t1 - list()

and then assign to *elements* of the list (using [[ ]] notation), not
to *sublists* of the list (which is what [ ] notation means in R),
like this:

for(i in 1:10){
   t1[[i]] - rnorm(250)
}

Is that what you had in mind?

   -s

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


[R] Loop question

2009-04-17 Thread Brendan Morse
Hi everyone, I am trying to accomplish a small task that is giving me  
quite a headache. I would like to automatically generate a series of  
matrices and give them successive names. Here is what I thought at  
first:


t1-matrix(0, nrow=250, ncol=1)

for(i in 1:10){
t1[i]-rnorm(250)
}

What I intended was that the loop would create 10 different matrices  
with a single column of 250 values randomly selected from a normal  
distribution, and that they would be labeled t11, t12, t13, t14 etc.


Can anyone steer me in the right direction with this one?

Thanks!
Brendan

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


Re: [R] Loop question

2009-04-17 Thread Jorge Ivan Velez
Dear Brendan,
One way could be either

bigt - sapply(1:10,function(x) rnorm(250))
colnames(bigt) - paste('t',1:10,sep=)
bigt

or

bigt2 - NULL
for(i in 1:10) bigt2 - cbind( bigt2, rnorm(250) )
colnames(bigt2) - paste('t',1:10,sep=)
bigt2

or

bigt3 - matrix(rnorm(250*10),ncol=10)
colnames(bigt3) - paste('t',1:10,sep=)
bigt3


HTH,

Jorge



On Fri, Apr 17, 2009 at 10:12 PM, Brendan Morse morse.bren...@gmail.comwrote:

 Hi everyone, I am trying to accomplish a small task that is giving me quite
 a headache. I would like to automatically generate a series of matrices and
 give them successive names. Here is what I thought at first:

 t1-matrix(0, nrow=250, ncol=1)

 for(i in 1:10){
t1[i]-rnorm(250)
 }

 What I intended was that the loop would create 10 different matrices with a
 single column of 250 values randomly selected from a normal distribution,
 and that they would be labeled t11, t12, t13, t14 etc.

 Can anyone steer me in the right direction with this one?

 Thanks!
 Brendan

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


[[alternative HTML version deleted]]

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


Re: [R] Loop question

2009-04-17 Thread Ben Bolker



Brendan Morse wrote:
 
 Hi everyone, I am trying to accomplish a small task that is giving me  
 quite a headache. I would like to automatically generate a series of  
 matrices and give them successive names. Here is what I thought at  
 first:
 
 t1-matrix(0, nrow=250, ncol=1)
 
 for(i in 1:10){
   t1[i]-rnorm(250)
 }
 
 What I intended was that the loop would create 10 different matrices  
 with a single column of 250 values randomly selected from a normal  
 distribution, and that they would be labeled t11, t12, t13, t14 etc.
 

I think you're basically looking for

http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f

or

http://wiki.r-project.org/rwiki/doku.php?id=tips:data-misc:create_var_names

but see the comments in both places that indicate why it may be easier to do
this
as a list.

for(i in 1:10){
assign(paste(t1,i,sep=),matrix(rnorm(250)))
}


-- 
View this message in context: 
http://www.nabble.com/Loop-question-tp23108752p23108788.html
Sent from the R help mailing list archive at Nabble.com.

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