[R] for loop, error in model frame.default ... variable lengths differ

2012-05-21 Thread Peter Keller
I'm failing to get a for loop working.  I'm sure it's something simple, and I
have found some posts relating to it, but I'm just not understanding why
this isn't working. 

I have a data frame and would like to loop through specific column names,
using aggregate() within a for loop.  There are NA's scattered throughout
the data frame and I'm thinking it has something to do with that, but I
haven't been able to fix it.

vars - colnames(df)[c(10,12,16,18,20,21,24:29,45)]
 for(i in 1:length(vars)) {
aggregate(colnames(df)[i] ~ x1 + x2 + x3, df, mean,
na.action=na.exclude)
}

I get this error: 
Error in model.frame.default(formula = colnames(df)[i] ~ x1 + x2 +   : 
  variable lengths differ (found for 'x1')

There are probably much better ways to do this, and I would be happy to get
suggestions, but mostly I would like to know why the code isn't working.

Thanks-
Peter

--
View this message in context: 
http://r.789695.n4.nabble.com/for-loop-error-in-model-frame-default-variable-lengths-differ-tp4630698.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] for loop, error in model frame.default ... variable lengths differ

2012-05-21 Thread Jeff Newmiller
No data, not reproducible.

I think you should be using na.omit, though.
---
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.

Peter Keller keller...@gmail.com wrote:

I'm failing to get a for loop working.  I'm sure it's something simple,
and I
have found some posts relating to it, but I'm just not understanding
why
this isn't working. 

I have a data frame and would like to loop through specific column
names,
using aggregate() within a for loop.  There are NA's scattered
throughout
the data frame and I'm thinking it has something to do with that, but I
haven't been able to fix it.

vars - colnames(df)[c(10,12,16,18,20,21,24:29,45)]
 for(i in 1:length(vars)) {
aggregate(colnames(df)[i] ~ x1 + x2 + x3, df, mean,
na.action=na.exclude)
}

I get this error: 
Error in model.frame.default(formula = colnames(df)[i] ~ x1 + x2 +   : 
  variable lengths differ (found for 'x1')

There are probably much better ways to do this, and I would be happy to
get
suggestions, but mostly I would like to know why the code isn't
working.

Thanks-
Peter

--
View this message in context:
http://r.789695.n4.nabble.com/for-loop-error-in-model-frame-default-variable-lengths-differ-tp4630698.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.


Re: [R] for loop, error in model frame.default ... variable lengths differ

2012-05-21 Thread Petr PIKAL
Hi

You did not provide data but I can see some problems in your code. See 
inline.
 
 I'm failing to get a for loop working.  I'm sure it's something simple, 
and I
 have found some posts relating to it, but I'm just not understanding why
 this isn't working. 
 
 I have a data frame and would like to loop through specific column 
names,
 using aggregate() within a for loop.  There are NA's scattered 
throughout
 the data frame and I'm thinking it has something to do with that, but I
 haven't been able to fix it.
 
 vars - colnames(df)[c(10,12,16,18,20,21,24:29,45)]
  for(i in 1:length(vars)) {

So i is actually values from 1 to length of vars variable.

 aggregate(colnames(df)[i] ~ x1 + x2 + x3, df, mean,

and you select variables from df[,1] to df[, length(vars)], which is 
probably not what you want.
What is x1-x3? are they variables in df?

 na.action=na.exclude)

for mean the correct statement is na.rm=TRUE

 }
 
 I get this error: 
 Error in model.frame.default(formula = colnames(df)[i] ~ x1 + x2 +   : 
   variable lengths differ (found for 'x1')

Maybe x1 has different length as df. What length(x1) and dim(df) tells 
you?

Regards
Petr

 
 There are probably much better ways to do this, and I would be happy to 
get
 suggestions, but mostly I would like to know why the code isn't working.
 
 Thanks-
 Peter
 
 --
 View this message in context: http://r.789695.n4.nabble.com/for-loop-
 error-in-model-frame-default-variable-lengths-differ-tp4630698.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.


Re: [R] for loop, error in model frame.default ... variable lengths differ

2012-05-21 Thread peter dalgaard

On May 21, 2012, at 10:25 , Petr PIKAL wrote:

 Hi
 
 You did not provide data but I can see some problems in your code. See 
 inline.
 
 I'm failing to get a for loop working.  I'm sure it's something simple, 
 and I
 have found some posts relating to it, but I'm just not understanding why
 this isn't working. 
 
 I have a data frame and would like to loop through specific column 
 names,
 using aggregate() within a for loop.  There are NA's scattered 
 throughout
 the data frame and I'm thinking it has something to do with that, but I
 haven't been able to fix it.
 
 vars - colnames(df)[c(10,12,16,18,20,21,24:29,45)]
 for(i in 1:length(vars)) {
 
 So i is actually values from 1 to length of vars variable.
 
aggregate(colnames(df)[i] ~ x1 + x2 + x3, df, mean,
 
 and you select variables from df[,1] to df[, length(vars)], which is 
 probably not what you want.
 What is x1-x3? are they variables in df?
 
 na.action=na.exclude)
 
 for mean the correct statement is na.rm=TRUE
 
}
 
 I get this error: 
 Error in model.frame.default(formula = colnames(df)[i] ~ x1 + x2 +   : 
  variable lengths differ (found for 'x1')
 
 Maybe x1 has different length as df. What length(x1) and dim(df) tells 
 you?
 

colnames(df)[i] is a character vector of length 1. This doesn't work any better 
than

 aggregate(colnames(airquality)[1] ~ Month, airquality, mean, na.rm=T)
Error in model.frame.default(formula = colnames(airquality[1]) ~ Month,  : 
  variable lengths differ (found for 'Month')

What the poster probably wanted was something in the vein of

 nm - colnames(airquality)[1]
 ff - formula(bquote(.(as.name(nm))~Month))
 aggregate(ff, airquality, mean, na.rm=T)
  MonthOzone
1 5 23.61538
2 6 29.4
3 7 59.11538
4 8 59.96154
5 9 31.44828




 Regards
 Petr
 
 
 There are probably much better ways to do this, and I would be happy to 
 get
 suggestions, but mostly I would like to know why the code isn't working.
 
 Thanks-
 Peter
 
 --
 View this message in context: http://r.789695.n4.nabble.com/for-loop-
 error-in-model-frame-default-variable-lengths-differ-tp4630698.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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.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] for loop, error in model frame.default ... variable lengths differ

2012-05-21 Thread Joshua Wiley
On Mon, May 21, 2012 at 2:00 AM, peter dalgaard pda...@gmail.com wrote:
[snip]
 What the poster probably wanted was something in the vein of

 nm - colnames(airquality)[1]
 ff - formula(bquote(.(as.name(nm))~Month))
 aggregate(ff, airquality, mean, na.rm=T)
  Month    Ozone
 1     5 23.61538
 2     6 29.4
 3     7 59.11538
 4     8 59.96154
 5     9 31.44828

or perhaps to use an implicit loop (since looping seemed to be part of it all):

results - lapply(c(Ozone, Solar.R), function(n) {
  aggregate(. ~ Month, airquality[, c(n, Month)], mean, na.rm = TRUE)
  })

## print results
results


## untested code based on OPs original example
vars - colnames(df)[c(10,12,16,18,20,21,24:29,45)]
results - lapply(vars, function(n) {
  aggregate(. ~ x1 + x2 + x3, df[, c(n, x1, x2, x3)], mean, na.rm = TRUE)
  })

## print results
results

Cheers,

Josh

 --
 Peter Dalgaard, Professor
 Center for Statistics, Copenhagen Business School
 Solbjerg Plads 3, 2000 Frederiksberg, Denmark
 Phone: (+45)38153501
 Email: pd@cbs.dk  Priv: pda...@gmail.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.

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://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] For Loop Error

2012-01-29 Thread Rui Barradas
Hello,

 
 Every time the loop blows up, I get back this error:
 
 Error in table.combos[i, ] - table(combos) :
   number of items to replace is not a multiple of replacement length
 

This is because not all colors are present in that one sample that breaks
the code.

 
 There is no apparent consistency on which i in the loop I get this
 error.  
 
There shouldn't be, every time you run the loop, 'sample' will take
different values. To get
reproducibility you would need 'set.seed'.

A solution could be based on the following.
It's important to sort, like the difference between 'tc1' and 'tc2' proves.
It's 'tc2' you want.

Note that I've reduced the sample size to 10.
Note also that I initialize the matrices to zeros, not NA's.


color - sort(c('Br','Y','G','R','O','Bl'))

tc1 - matrix(0, nrow = 1, ncol = 6)
tc2 - matrix(0, nrow = 1, ncol = 6)

colnames(tc1) - color
colnames(tc2) - color

set.seed(123)
for(i in 1) {
  combos - sample(color, 10, replace = TRUE, prob = P.company)
  tc1[i, unique(combos)] - table(combos)
  tc2[i, sort(unique(combos))] - table(combos)
}

table(combos)
tc1
tc2


I hope this helps.

Rui Barradas


--
View this message in context: 
http://r.789695.n4.nabble.com/For-Loop-Error-tp4336585p4337469.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] For Loop Error

2012-01-29 Thread Petr Savicky
On Sat, Jan 28, 2012 at 10:25:47AM -0800, Melrose2012 wrote:
 Hi Again,
 
 I am writing a 'for loop' to create a matrix of randomly sampled colors. 
 I've written this loop in matlab and it works fine.  I then tried to do it
 in R and apparently there is something wrong with my syntax b/c every time I
 run the script, the for loop blows up at a different point in the loop.
 
 As you can see, I ask R to clear my workspace each time, so there shouldn't
 be any variables in my workspace that are messing this up.
 
 rm(list=ls())
 
 # Sample Size
 size - 53  
 # Probability of each color based on company
 P.company - c(.14,.14,.16,.13,.20,.24)
 # Names of colors
 color - c('Br','Y','G','R','O','Bl')
 # Make an empty matrix that will be filled in by for loop
 table.combos - matrix(nrow = 1, ncol = 6);
 
 # For loop will run through choosing a random bag of 53 MMs 1 times
 # and create a table enumerating the number of each color in each of these
 1 bags
 for(i in 1:1) {
   combos - sample(color,size, replace = TRUE, prob = P.company)
   table.combos[i, ] - table(combos)
   colnames(table.combos)-c(Br,Y,G,R,O,Bl)
 }
 
 Every time the loop blows up, I get back this error:
 
 Error in table.combos[i, ] - table(combos) : 
   number of items to replace is not a multiple of replacement length

Hi.

Rui Barradas already pointed out that the problem is with samples,
which do not contain all colors. Forcing table() to produce zero
frequencies of colors, which do not occur, a factor may be used.
Try, for example

  size - 53
  P.company - c(.14,.14,.16,.13,.20,.24)
  color - c('Br','Y','G','R','O','Bl')
  table.combos - matrix(nrow = 1, ncol = 6);
  colnames(table.combos) - color
 
  for(i in 1:1) {
combos - sample(color,size, replace = TRUE, prob = P.company)
table.combos[i, ] - table(factor(combos, levels=color))
  }

  which(table.combos==0, arr.ind=TRUE)

 row col
   [1,]  588   1
   [2,] 3005   1
   [3,] 4535   1
   [4,] 8314   1
   [5,] 7654   2
   [6,] 8607   3
   [7,] 1790   4
   [8,] 1980   4
   [9,] 2991   4
  [10,] 4550   4
  [11,] 5868   4
  [12,] 6704   4
  [13,] 8769   4
  [14,] 8823   4
  [15,] 7861   5

Hope this helps.

Petr Savicky.

__
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] For Loop Error

2012-01-28 Thread Melrose2012
Hi Again,

I am writing a 'for loop' to create a matrix of randomly sampled colors. 
I've written this loop in matlab and it works fine.  I then tried to do it
in R and apparently there is something wrong with my syntax b/c every time I
run the script, the for loop blows up at a different point in the loop.

As you can see, I ask R to clear my workspace each time, so there shouldn't
be any variables in my workspace that are messing this up.

rm(list=ls())

# Sample Size
size - 53  
# Probability of each color based on company
P.company - c(.14,.14,.16,.13,.20,.24)
# Names of colors
color - c('Br','Y','G','R','O','Bl')
# Make an empty matrix that will be filled in by for loop
table.combos - matrix(nrow = 1, ncol = 6);

# For loop will run through choosing a random bag of 53 MMs 1 times
# and create a table enumerating the number of each color in each of these
1 bags
for(i in 1:1) {
  combos - sample(color,size, replace = TRUE, prob = P.company)
  table.combos[i, ] - table(combos)
  colnames(table.combos)-c(Br,Y,G,R,O,Bl)
}

Every time the loop blows up, I get back this error:

Error in table.combos[i, ] - table(combos) : 
  number of items to replace is not a multiple of replacement length

There is no apparent consistency on which i in the loop I get this error. 
Sometimes i = 10, sometimes i = 685, sometimes i = 1954, sometimes i = 59,
etc.

If anyone can please tell me what I am doing wrong, it would be greatly
appreciated!

Cheers,
Melissa

--
View this message in context: 
http://r.789695.n4.nabble.com/For-Loop-Error-tp4336585p4336585.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] crazy loop error.

2011-01-26 Thread Petr Savicky
On Tue, Jan 25, 2011 at 08:58:31AM +, Prof Brian Ripley wrote:
[...]
 If k may be 0, then it is better to use
 
  for (n in seq(length=k))
 
 since seq(length=0) has length 0.
 
 Since you keep mentioning that, it is actually much better to use 
 seq_len(k) (and seq_along(x) instead of your earlier recommendation of 
 seq(along=x)).  And if you are using seq() in other cases in programs, 
 consider seq.int() instead.

Thank you for pointing out the functions seq_len(), seq_along() and
seq.int(). These functions are primitive and faster, as others already
mentioned. Using replicate(), i obtained on my computer a speed up by a
factor between 5 and 7 for k = 20 and there is a remarkable speed up
also for larger k. The function seq.int() is more general than the other
two. In particular, it can generate also a decreasing sequence.

Petr Savicky.

__
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] crazy loop error.

2011-01-25 Thread Petr Savicky
On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:
 Thanks for the reply Erik, As you mentioned, grouping consecutive elements
 of 'a' was my idea.
 I am unaware of any R'ish way to do it. It would be nice if someone in the
 community knows this.
 
 The error resulting in the NA was pretty easy to fix, and my loop works, but
 the results are still wrong (new script below).
 Ideally it should print single hello for the single letters and grouped '3
 hellos' for the fives, grouped '2 hellos' for the sixes etc.
 
 Based on the run results, if the value of n is being tracked, it changes
 quite unpredictably.
 Can someone explain how the value of n changes from end of the loop to the
 top without anything being done to it?

Hi.

A for-loop in R is different from a for-loop in C. It is similar
to foreach loop in Perl. If v is a vector, then

  for (n in v)

first creates the vector v and then always performs length(v) iterations.
Before iteration i, n is assigned v[i] even if n is changed in the
previous iteration.

If you want to control the loop variable during execution, it is possible
to use a while loop, where you have full control. While loop may be better
also if v has a very large length, since, for example

  for (n in 1:100)

creates a vector of length 100 in memory.

It should also be noted that the for-loop

  for (n in 1:k)

performs 2 iterations, if k is 0, since 1:0 is a vector of length 2.
If k may be 0, then it is better to use

  for (n in seq(length=k))

since seq(length=0) has length 0.

Hope this helps.

Petr Savicky.

__
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] crazy loop error.

2011-01-25 Thread Petr PIKAL
Hi


r-help-boun...@r-project.org napsal dne 24.01.2011 23:18:35:

 Thanks for the reply Erik, As you mentioned, grouping consecutive 
elements
 of 'a' was my idea.
 I am unaware of any R'ish way to do it. It would be nice if someone in 
the
 community knows this.
 
 The error resulting in the NA was pretty easy to fix, and my loop works, 
but
 the results are still wrong (new script below).
 Ideally it should print single hello for the single letters and 
grouped '3
 hellos' for the fives, grouped '2 hellos' for the sixes etc.
 
 Based on the run results, if the value of n is being tracked, it changes
 quite unpredictably.
 Can someone explain how the value of n changes from end of the loop to 
the
 top without anything being done to it?

Put it into a foo function and make use debug(fun())

Regards
Petr

BTW mapply solution is shorter and probably quicker and easier to 
maintain.

 I cannot figure out what I am doing wrong.
 
 a-c(2,3,5,5,5,6,6,7)
 
 for(n in 1:length(a))
 {
 print(paste(n: ,n))
 z1-a[n]
 print(paste(z1:,z1))
 #make a list container
 ldata-list()
 t=1
 
 while(z1==a[n])
 {
 
 #add dataframes to list
 ldata[[t]]-paste(hello)
 
 n=n+1
 t=t+1
 
 if(nlength(a))
 {
 break;
 }
 }
 print(--End of while loop---)
 
 for(y in 1:length(ldata))
 {
 print(ldata[[y]])
 }
 
 print(paste(n: ,n))
 print(**End of for loop)
 }
 
 
 Thanks,
 Roy
 
[[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] crazy loop error.

2011-01-25 Thread Prof Brian Ripley

On Tue, 25 Jan 2011, Petr Savicky wrote:


On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:

Thanks for the reply Erik, As you mentioned, grouping consecutive elements
of 'a' was my idea.
I am unaware of any R'ish way to do it. It would be nice if someone in the
community knows this.

The error resulting in the NA was pretty easy to fix, and my loop works, but
the results are still wrong (new script below).
Ideally it should print single hello for the single letters and grouped '3
hellos' for the fives, grouped '2 hellos' for the sixes etc.

Based on the run results, if the value of n is being tracked, it changes
quite unpredictably.
Can someone explain how the value of n changes from end of the loop to the
top without anything being done to it?


Hi.

A for-loop in R is different from a for-loop in C. It is similar
to foreach loop in Perl. If v is a vector, then

 for (n in v)

first creates the vector v and then always performs length(v) iterations.
Before iteration i, n is assigned v[i] even if n is changed in the
previous iteration.


And also if v is changed during the loop.


If you want to control the loop variable during execution, it is possible
to use a while loop, where you have full control. While loop may be better
also if v has a very large length, since, for example

 for (n in 1:100)

creates a vector of length 100 in memory.

It should also be noted that the for-loop

 for (n in 1:k)

performs 2 iterations, if k is 0, since 1:0 is a vector of length 2.
If k may be 0, then it is better to use

 for (n in seq(length=k))

since seq(length=0) has length 0.


Since you keep mentioning that, it is actually much better to use 
seq_len(k) (and seq_along(x) instead of your earlier recommendation of 
seq(along=x)).  And if you are using seq() in other cases in programs, 
consider seq.int() instead.



Hope this helps.

Petr Savicky.


--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
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] crazy loop error.

2011-01-25 Thread Petr Savicky
On Tue, Jan 25, 2011 at 09:05:03AM +0100, Petr Savicky wrote:
[...]
 to foreach loop in Perl. If v is a vector, then
 
   for (n in v)
 
 first creates the vector v and then always performs length(v) iterations.

I forgot that ‘break’ may stop the loop. See ?for for further
information. In particular, it says

  You can assign to ‘var’ within the body of the loop, but
  this will not affect the next iteration.

Petr Savicky.

__
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] crazy loop error.

2011-01-25 Thread Roy Mathew
Dear Erik,
Thanks for the mapply idea. I never got around to understand all those apply
functions.
I am still curious as to why the other loop didnt work. I even tried the
debug but doesnt help.
Anyway I will leave that for now.
Thanks a lot for your help.
Regards,
Roy


On Mon, Jan 24, 2011 at 11:43 PM, Erik Iverson er...@ccbr.umn.edu wrote:



 Roy Mathew wrote:

 Thanks for the reply Erik, As you mentioned, grouping consecutive elements
 of 'a' was my idea. I am unaware of any R'ish way to do it. It would be nice
 if someone in the community knows this.


 Is this the idea you're trying to execute?  It uses ?rle and ?mapply.


 a - c(2,3,5,5,5,6,6,7)
 mapply(rep, hello, rle(a)$lengths, USE.NAMES = FALSE)

 [[1]]
 [1] hello

 [[2]]
 [1] hello

 [[3]]
 [1] hello hello hello

 [[4]]
 [1] hello hello

 [[5]]
 [1] hello




-- 
Best Regards,
Roy

[[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] crazy loop error.

2011-01-25 Thread Ivan Calandra

Mr Ripley,

May I ask why seq_len() and seq_along() are better than seq()?

Thanks,
Ivan

Le 1/25/2011 09:58, Prof Brian Ripley a écrit :

On Tue, 25 Jan 2011, Petr Savicky wrote:


On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:
Thanks for the reply Erik, As you mentioned, grouping consecutive 
elements

of 'a' was my idea.
I am unaware of any R'ish way to do it. It would be nice if someone 
in the

community knows this.

The error resulting in the NA was pretty easy to fix, and my loop 
works, but

the results are still wrong (new script below).
Ideally it should print single hello for the single letters and 
grouped '3

hellos' for the fives, grouped '2 hellos' for the sixes etc.

Based on the run results, if the value of n is being tracked, it 
changes

quite unpredictably.
Can someone explain how the value of n changes from end of the loop 
to the

top without anything being done to it?


Hi.

A for-loop in R is different from a for-loop in C. It is similar
to foreach loop in Perl. If v is a vector, then

 for (n in v)

first creates the vector v and then always performs length(v) 
iterations.

Before iteration i, n is assigned v[i] even if n is changed in the
previous iteration.


And also if v is changed during the loop.

If you want to control the loop variable during execution, it is 
possible
to use a while loop, where you have full control. While loop may be 
better

also if v has a very large length, since, for example

 for (n in 1:100)

creates a vector of length 100 in memory.

It should also be noted that the for-loop

 for (n in 1:k)

performs 2 iterations, if k is 0, since 1:0 is a vector of length 2.
If k may be 0, then it is better to use

 for (n in seq(length=k))

since seq(length=0) has length 0.


Since you keep mentioning that, it is actually much better to use 
seq_len(k) (and seq_along(x) instead of your earlier recommendation of 
seq(along=x)).  And if you are using seq() in other cases in programs, 
consider seq.int() instead.



Hope this helps.

Petr Savicky.




--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

__
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] crazy loop error.

2011-01-25 Thread Roy Mathew
ooh.. I have another question.
What if I want to add the value in the vector a to the hello each time it
prints.
Here is your output

a - c(2,3,5,5,5,6,6,7)
mapply(rep, hello, rle(a)$lengths, USE.NAMES = FALSE)

[[1]]
[1] hello

[[2]]
[1] hello

[[3]]
[1] hello hello hello

[[4]]
[1] hello hello

[[5]]
[1] hello

If I want something like this, based on values in vector a.

[[1]]
[1] hello 2

[[2]]
[1] hello 3

[[3]]
[1] hello 5 hello 5 hello 5

[[4]]
[1] hello 6 hello 6

[[5]]
[1] hello 7

What i am actually doing is hmm.. I have a bunch of text files which is
output from another program. I want to extract some specific information
from these files and write to a new file and save it.
All these files have a certain variable k which maybe 2, or 3 or 5 etc. The
vector a shows the k values of 8 of such files. I want the contents of all
files with k value 5 to be written into one file.

Thanks,
Roy



On Mon, Jan 24, 2011 at 11:43 PM, Erik Iverson er...@ccbr.umn.edu wrote:



 Roy Mathew wrote:

 Thanks for the reply Erik, As you mentioned, grouping consecutive elements
 of 'a' was my idea. I am unaware of any R'ish way to do it. It would be nice
 if someone in the community knows this.


 Is this the idea you're trying to execute?  It uses ?rle and ?mapply.


 a - c(2,3,5,5,5,6,6,7)
 mapply(rep, hello, rle(a)$lengths, USE.NAMES = FALSE)

 [[1]]
 [1] hello

 [[2]]
 [1] hello

 [[3]]
 [1] hello hello hello

 [[4]]
 [1] hello hello

 [[5]]
 [1] hello




-- 
Best Regards,
Roy

[[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] crazy loop error.

2011-01-25 Thread Petr PIKAL
Hi

r-help-boun...@r-project.org napsal dne 25.01.2011 10:58:36:

 ooh.. I have another question.
 What if I want to add the value in the vector a to the hello each time 
it
 prints.
 Here is your output
 
 a - c(2,3,5,5,5,6,6,7)
 mapply(rep, hello, rle(a)$lengths, USE.NAMES = FALSE)
 
 [[1]]
 [1] hello
 
 [[2]]
 [1] hello
 
 [[3]]
 [1] hello hello hello
 
 [[4]]
 [1] hello hello
 
 [[5]]
 [1] hello
 
 If I want something like this, based on values in vector a.

Not sure how to use mapply

test-vector(list, 5)
lll-rle(a)
for (i in seq_along(lll$lengths)) test[[i]] - rep(paste(hello, 
lll$values[i]), lll$lengths[i])


 
 [[1]]
 [1] hello 2
 
 [[2]]
 [1] hello 3
 
 [[3]]
 [1] hello 5 hello 5 hello 5
 
 [[4]]
 [1] hello 6 hello 6
 
 [[5]]
 [1] hello 7
 
 What i am actually doing is hmm.. I have a bunch of text files which is
 output from another program. I want to extract some specific information
 from these files and write to a new file and save it.
 All these files have a certain variable k which maybe 2, or 3 or 5 etc. 
The
 vector a shows the k values of 8 of such files. I want the contents of 
all
 files with k value 5 to be written into one file.

That is rather vague description. Does those files have some structure? 
How do you know the variable k?

Loops are not so ineffective if you use them for what they are good and if 
you do not expand the object within loop. See R-Inferno from P.Burns.

Regards
Petr




 
 Thanks,
 Roy
 
 
 
 On Mon, Jan 24, 2011 at 11:43 PM, Erik Iverson er...@ccbr.umn.edu 
wrote:
 
 
 
  Roy Mathew wrote:
 
  Thanks for the reply Erik, As you mentioned, grouping consecutive 
elements
  of 'a' was my idea. I am unaware of any R'ish way to do it. It would 
be nice
  if someone in the community knows this.
 
 
  Is this the idea you're trying to execute?  It uses ?rle and ?mapply.
 
 
  a - c(2,3,5,5,5,6,6,7)
  mapply(rep, hello, rle(a)$lengths, USE.NAMES = FALSE)
 
  [[1]]
  [1] hello
 
  [[2]]
  [1] hello
 
  [[3]]
  [1] hello hello hello
 
  [[4]]
  [1] hello hello
 
  [[5]]
  [1] hello
 
 
 
 
 -- 
 Best Regards,
 Roy
 
[[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] crazy loop error.

2011-01-25 Thread Bert Gunter
Well, I'm not Prof. Ripley, but the answer is: Look at the code.
seq_len, seq.int, and seq_along call Primitives, which are implemented
in C, and therefore MUCH faster than seq(), which is implemented as
pure R code (and is also a generic, so requires method dispatch).
Though for small n (up to a few thousand, say), it probably doesn't
make much difference.(Here, to be corrected by Prof. Ripley is
needed).

-- Bert

On Tue, Jan 25, 2011 at 2:22 AM, Ivan Calandra
ivan.calan...@uni-hamburg.de wrote:
 Mr Ripley,

 May I ask why seq_len() and seq_along() are better than seq()?

 Thanks,
 Ivan

 Le 1/25/2011 09:58, Prof Brian Ripley a écrit :

 On Tue, 25 Jan 2011, Petr Savicky wrote:

 On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:

 Thanks for the reply Erik, As you mentioned, grouping consecutive
 elements
 of 'a' was my idea.
 I am unaware of any R'ish way to do it. It would be nice if someone in
 the
 community knows this.

 The error resulting in the NA was pretty easy to fix, and my loop works,
 but
 the results are still wrong (new script below).
 Ideally it should print single hello for the single letters and
 grouped '3
 hellos' for the fives, grouped '2 hellos' for the sixes etc.

 Based on the run results, if the value of n is being tracked, it changes
 quite unpredictably.
 Can someone explain how the value of n changes from end of the loop to
 the
 top without anything being done to it?

 Hi.

 A for-loop in R is different from a for-loop in C. It is similar
 to foreach loop in Perl. If v is a vector, then

  for (n in v)

 first creates the vector v and then always performs length(v) iterations.
 Before iteration i, n is assigned v[i] even if n is changed in the
 previous iteration.

 And also if v is changed during the loop.

 If you want to control the loop variable during execution, it is possible
 to use a while loop, where you have full control. While loop may be
 better
 also if v has a very large length, since, for example

  for (n in 1:100)

 creates a vector of length 100 in memory.

 It should also be noted that the for-loop

  for (n in 1:k)

 performs 2 iterations, if k is 0, since 1:0 is a vector of length 2.
 If k may be 0, then it is better to use

  for (n in seq(length=k))

 since seq(length=0) has length 0.

 Since you keep mentioning that, it is actually much better to use
 seq_len(k) (and seq_along(x) instead of your earlier recommendation of
 seq(along=x)).  And if you are using seq() in other cases in programs,
 consider seq.int() instead.

 Hope this helps.

 Petr Savicky.


 --
 Ivan CALANDRA
 PhD Student
 University of Hamburg
 Biozentrum Grindel und Zoologisches Museum
 Abt. Säugetiere
 Martin-Luther-King-Platz 3
 D-20146 Hamburg, GERMANY
 +49(0)40 42838 6231
 ivan.calan...@uni-hamburg.de

 **
 http://www.for771.uni-bonn.de
 http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

 __
 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.




-- 
Bert Gunter
Genentech Nonclinical Biostatistics

__
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] crazy loop error.

2011-01-25 Thread Ivan Calandra
Now I understand what the difference between a primitive and a 
non-primitive!

Thanks for the clarification!
Ivan

Le 1/25/2011 18:03, Bert Gunter a écrit :

Well, I'm not Prof. Ripley, but the answer is: Look at the code.
seq_len, seq.int, and seq_along call Primitives, which are implemented
in C, and therefore MUCH faster than seq(), which is implemented as
pure R code (and is also a generic, so requires method dispatch).
Though for small n (up to a few thousand, say), it probably doesn't
make much difference.(Here, to be corrected by Prof. Ripley is
needed).

-- Bert

On Tue, Jan 25, 2011 at 2:22 AM, Ivan Calandra
ivan.calan...@uni-hamburg.de  wrote:

Mr Ripley,

May I ask why seq_len() and seq_along() are better than seq()?

Thanks,
Ivan

Le 1/25/2011 09:58, Prof Brian Ripley a écrit :

On Tue, 25 Jan 2011, Petr Savicky wrote:


On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote:

Thanks for the reply Erik, As you mentioned, grouping consecutive
elements
of 'a' was my idea.
I am unaware of any R'ish way to do it. It would be nice if someone in
the
community knows this.

The error resulting in the NA was pretty easy to fix, and my loop works,
but
the results are still wrong (new script below).
Ideally it should print single hello for the single letters and
grouped '3
hellos' for the fives, grouped '2 hellos' for the sixes etc.

Based on the run results, if the value of n is being tracked, it changes
quite unpredictably.
Can someone explain how the value of n changes from end of the loop to
the
top without anything being done to it?

Hi.

A for-loop in R is different from a for-loop in C. It is similar
to foreach loop in Perl. If v is a vector, then

  for (n in v)

first creates the vector v and then always performs length(v) iterations.
Before iteration i, n is assigned v[i] even if n is changed in the
previous iteration.

And also if v is changed during the loop.


If you want to control the loop variable during execution, it is possible
to use a while loop, where you have full control. While loop may be
better
also if v has a very large length, since, for example

  for (n in 1:100)

creates a vector of length 100 in memory.

It should also be noted that the for-loop

  for (n in 1:k)

performs 2 iterations, if k is 0, since 1:0 is a vector of length 2.
If k may be 0, then it is better to use

  for (n in seq(length=k))

since seq(length=0) has length 0.

Since you keep mentioning that, it is actually much better to use
seq_len(k) (and seq_along(x) instead of your earlier recommendation of
seq(along=x)).  And if you are using seq() in other cases in programs,
consider seq.int() instead.


Hope this helps.

Petr Savicky.

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

__
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.






--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

__
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] crazy loop error.

2011-01-24 Thread Roy Mathew
Dear R-users,
This is a loop which is part of a bigger script. I managed to isolate the
error in this loop and simplified it to the bare minimum and made it
self-contained.

a-c(2,3,4,5,5,5,6,6,6,7)

for(n in 1:10)
{
print(paste(n: ,n))
z1-a[n]
#make a list container
ldata-list()
t=1

while(z1==a[n])
{

#add dataframes to list
ldata[[t]]-paste(hello)

n=n+1
t=t+1

}
print(--End of while loop---)

for(y in 1:length(ldata))
{
print(ldata[[y]])
}

print(paste(n: ,n))
print(**End of for loop)
}


This script has a vector a, for-loop, and a nested while-loop.
The for-loop runs from 1 to length of a. At every number of a, it enters the
while-loop and a hello is saved into list ldata.
If the next number in the vector a is a different number from previous then
the while-loop is exited and saved hello is printed.
If the next number in vector a is same as before then it loops inside the
while-loop and several hellos are printed together.

Then run-time error is

Error in while (z1 == a[n]) { : missing value where TRUE/FALSE needed

Thats because an NA creeps in somewhere. The problem can be seen far before
that. The full output from the run is below.
A lot of stuff was printed to help with the debugging. At n=4, there are
three repeats of 5, therefore hello is printed 3 times. n then becomes 7.
Then when the for-loop returns to top, n miraculously becomes 5. Hows
that!!?? Then on, everything goes wrong. I cannot figure out the problem.

[1] n:  1
[1] --End of while loop---
[1] hello
[1] n:  2
[1] **End of for loop
[1] n:  2
[1] --End of while loop---
[1] hello
[1] n:  3
[1] **End of for loop
[1] n:  3
[1] --End of while loop---
[1] hello
[1] n:  4
[1] **End of for loop
[1] n:  4
[1] --End of while loop---
[1] hello
[1] hello
[1] hello
[1] n:  7
[1] **End of for loop
[1] n:  5
[1] --End of while loop---
[1] hello
[1] hello
[1] n:  7
[1] **End of for loop
[1] n:  6
[1] --End of while loop---
[1] hello
[1] n:  7
[1] **End of for loop
[1] n:  7
[1] --End of while loop---
[1] hello
[1] hello
[1] hello
[1] n:  10
[1] **End of for loop
[1] n:  8
[1] --End of while loop---
[1] hello
[1] hello
[1] n:  10
[1] **End of for loop
[1] n:  9
[1] --End of while loop---
[1] hello
[1] n:  10
[1] **End of for loop
[1] n:  10
Error in while (z1 == a[n]) { : missing value where TRUE/FALSE needed

Mr Stuck-up.
Thanks for any help.
Roy

[[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] crazy loop error.

2011-01-24 Thread Erik Iverson

Roy,

I have no idea what you're actually trying to do here, but
it looks like there would be a more natural R'ish way if
you're concerned about grouping consecutive elements of 'a'.

At any rate, within your while loop, you're incrementing n by
1, and eventually n will be 10, which will be transformed to
11 when you add 1 to it, and a[11] will be NA, thus the error
you receive...


Roy Mathew wrote:

Dear R-users,
This is a loop which is part of a bigger script. I managed to isolate the
error in this loop and simplified it to the bare minimum and made it
self-contained.

a-c(2,3,4,5,5,5,6,6,6,7)

for(n in 1:10)
{
print(paste(n: ,n))
z1-a[n]
#make a list container
ldata-list()
t=1

while(z1==a[n])
{

#add dataframes to list
ldata[[t]]-paste(hello)

n=n+1
t=t+1

}
print(--End of while loop---)

for(y in 1:length(ldata))
{
print(ldata[[y]])
}

print(paste(n: ,n))
print(**End of for loop)
}


This script has a vector a, for-loop, and a nested while-loop.
The for-loop runs from 1 to length of a. At every number of a, it enters the
while-loop and a hello is saved into list ldata.
If the next number in the vector a is a different number from previous then
the while-loop is exited and saved hello is printed.
If the next number in vector a is same as before then it loops inside the
while-loop and several hellos are printed together.

Then run-time error is

Error in while (z1 == a[n]) { : missing value where TRUE/FALSE needed

Thats because an NA creeps in somewhere. The problem can be seen far before
that. The full output from the run is below.
A lot of stuff was printed to help with the debugging. At n=4, there are
three repeats of 5, therefore hello is printed 3 times. n then becomes 7.
Then when the for-loop returns to top, n miraculously becomes 5. Hows
that!!?? Then on, everything goes wrong. I cannot figure out the problem.

[1] n:  1
[1] --End of while loop---
[1] hello
[1] n:  2
[1] **End of for loop
[1] n:  2
[1] --End of while loop---
[1] hello
[1] n:  3
[1] **End of for loop
[1] n:  3
[1] --End of while loop---
[1] hello
[1] n:  4
[1] **End of for loop
[1] n:  4
[1] --End of while loop---
[1] hello
[1] hello
[1] hello
[1] n:  7
[1] **End of for loop
[1] n:  5
[1] --End of while loop---
[1] hello
[1] hello
[1] n:  7
[1] **End of for loop
[1] n:  6
[1] --End of while loop---
[1] hello
[1] n:  7
[1] **End of for loop
[1] n:  7
[1] --End of while loop---
[1] hello
[1] hello
[1] hello
[1] n:  10
[1] **End of for loop
[1] n:  8
[1] --End of while loop---
[1] hello
[1] hello
[1] n:  10
[1] **End of for loop
[1] n:  9
[1] --End of while loop---
[1] hello
[1] n:  10
[1] **End of for loop
[1] n:  10
Error in while (z1 == a[n]) { : missing value where TRUE/FALSE needed

Mr Stuck-up.
Thanks for any help.
Roy

[[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] crazy loop error.

2011-01-24 Thread Petr Savicky
On Mon, Jan 24, 2011 at 07:16:58PM +0100, Roy Mathew wrote:
 Dear R-users,
 This is a loop which is part of a bigger script. I managed to isolate the
 error in this loop and simplified it to the bare minimum and made it
 self-contained.
 
 a-c(2,3,4,5,5,5,6,6,6,7)
 
 for(n in 1:10)
 {
 print(paste(n: ,n))
 z1-a[n]
 #make a list container
 ldata-list()
 t=1
 
 while(z1==a[n])
 {
 
 #add dataframes to list
 ldata[[t]]-paste(hello)
 
 n=n+1
 t=t+1
 
 }
 print(--End of while loop---)
 
 for(y in 1:length(ldata))
 {
 print(ldata[[y]])
 }
 
 print(paste(n: ,n))
 print(**End of for loop)
 }
 
 
 This script has a vector a, for-loop, and a nested while-loop.
 The for-loop runs from 1 to length of a. At every number of a, it enters the
 while-loop and a hello is saved into list ldata.
 If the next number in the vector a is a different number from previous then
 the while-loop is exited and saved hello is printed.
 If the next number in vector a is same as before then it loops inside the
 while-loop and several hellos are printed together.
 
 Then run-time error is
 
 Error in while (z1 == a[n]) { : missing value where TRUE/FALSE needed
 
 Thats because an NA creeps in somewhere. The problem can be seen far before
 that. The full output from the run is below.
 A lot of stuff was printed to help with the debugging. At n=4, there are
 three repeats of 5, therefore hello is printed 3 times. n then becomes 7.
 Then when the for-loop returns to top, n miraculously becomes 5. Hows
 that!!??

Hi.

The for-loop for (i in 1:k) uses an internal index, which counts
the repetitions. This is necessary, since the control over a loop
like for (i in c(1,1,1,1)) cannot be based on the variable i only.
Hence, changing i does not influence the next iteration of the loop.
For example, the following loop always makes m*n repetitions, although
using the same variable in nested loops is definitely not suggested.

  m - 3
  n - 5
  for (i in seq(length=m)) {
  for (i in seq(length=n)) {
  cat(*)
  }
  cat(\n)
  }

Hope this helps.

Petr Savicky.

__
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] crazy loop error.

2011-01-24 Thread Erik Iverson



Roy Mathew wrote:
Thanks for the reply Erik, As you mentioned, grouping consecutive 
elements of 'a' was my idea. 
I am unaware of any R'ish way to do it. It would be nice if someone in 
the community knows this.


Is this the idea you're trying to execute?  It uses ?rle and ?mapply.

a - c(2,3,5,5,5,6,6,7)
mapply(rep, hello, rle(a)$lengths, USE.NAMES = FALSE)

[[1]]
[1] hello

[[2]]
[1] hello

[[3]]
[1] hello hello hello

[[4]]
[1] hello hello

[[5]]
[1] hello

__
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] crazy loop error.

2011-01-24 Thread Roy Mathew
Thanks for the reply Erik, As you mentioned, grouping consecutive elements
of 'a' was my idea.
I am unaware of any R'ish way to do it. It would be nice if someone in the
community knows this.

The error resulting in the NA was pretty easy to fix, and my loop works, but
the results are still wrong (new script below).
Ideally it should print single hello for the single letters and grouped '3
hellos' for the fives, grouped '2 hellos' for the sixes etc.

Based on the run results, if the value of n is being tracked, it changes
quite unpredictably.
Can someone explain how the value of n changes from end of the loop to the
top without anything being done to it?
I cannot figure out what I am doing wrong.

a-c(2,3,5,5,5,6,6,7)

for(n in 1:length(a))
{
print(paste(n: ,n))
z1-a[n]
print(paste(z1:,z1))
#make a list container
ldata-list()
t=1

while(z1==a[n])
{

#add dataframes to list
ldata[[t]]-paste(hello)

n=n+1
t=t+1

if(nlength(a))
{
break;
}
}
print(--End of while loop---)

for(y in 1:length(ldata))
{
print(ldata[[y]])
}

print(paste(n: ,n))
print(**End of for loop)
}


Thanks,
Roy

[[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.