That's fantastic, thank you very much, the qnorm option is interesting, I
will have to play around with it.

Many thanks again
Philip

-----Original Message-----
From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com] 
Sent: Sunday, 23 October 2011 10:28 AM
To: Philip Robinson
Cc: r-help@r-project.org
Subject: Re: [R] R for loop stops after 4 iterations

There's a seeming inconsistency in this question -- namely,  you provide an
example of a data frame with 4 columns but say it is 27x3
-- but I think your question comes from a misunderstanding of what
length(e) calculates. For a data frame it gives the number of columns back.
Hence if you have a 27x4 data frame (which you appear to) iterations will
only fill the first four elements of output.

You'd probably rather use NROW(e). As an aside, for these sort of loops,
seq_along() is usually a very good choice, but it doesn't work here because
of the length() thing.

On another note, why don't you just do the calculation analytically and save
yourself some trouble?


# Something like
with(e, qnorm(0.42, V2, V3)*100)


Michael


On Sat, Oct 22, 2011 at 7:33 PM, Philip Robinson
<philip.c.robin...@gmail.com> wrote:
> I have a data frame called e, dim is 27,3, the first 5 lines look like
this:
>
>
>
>
>
>     V1   V2   V3        V4
>
> 1  1673 0.36 0.08  Smith
>
> 2 167 0.36 0.08     Allen
>
> 3    99 0.37 0.06     Allen
>
> 4   116 0.38 0.07     Allen
>
> 5    95 0.41 0.08     Allen
>
>
>
> I am trying to calculate the proportion/percentage of V1 which would 
> have values >0.42 if V2 was the mean of a normal distribution with V1 
> people and a standard distribution of V3. The loop works but only for 
> 4 iterations then stops, I can't understand why, the code and the 
> output are below
>
>
>
>
>
> output <- rep(NA, 27)
>
> for (i in 1:length(e))
>
> {
>
> x <- rnorm(n=e[i,1], mean=e[i,2], sd=e[i,3])
>
> n <- e[i,1]
>
> v <- x>0.42
>
> q <-(sum(v)/n)*100
>
> output[i] <- q
>
> }
>
>
>
>>output
>
> [1] 22.23551 27.54491 25.25253 19.82759       NA       NA       NA       
> NA NA
>
> [10]       NA       NA       NA       NA       NA       NA       NA       
> NA NA
>
> [19]       NA       NA       NA       NA       NA       NA       NA       
> NA NA
>
>
>        [[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.

Reply via email to