Re: [R] nested loop

2017-09-09 Thread Ek Esawi
I would try fininterval as well. It should do what you have asked provided
that you take care of the issue Ulrik pointed out.
Best of luck--EK

On Fri, Sep 8, 2017 at 6:15 AM, Hemant Sain  wrote:

> i have a vector containing values ranging from 0 to 24
> i want to create another variable which can categorize those values  like
> this
> please help me with an R code
>
> Thanks
>
> *Value   New_Var*10 -5
> 30 -5
> 50 -5
> 96-10
> 76-10
> 56-10
> 4 0-5
> 11  11-15
> 12 11-15
> 18  16-20
> 23  21 -25
>
> --
> hemantsain.com
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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] nested loop

2017-09-08 Thread Hemant Sain
i have a vector containing values ranging from 0 to 24
i want to create another variable which can categorize those values  like
this
please help me with an R code

Thanks

*Value   New_Var*10 -5
30 -5
50 -5
96-10
76-10
56-10
4 0-5
11  11-15
12 11-15
18  16-20
23  21 -25

-- 
hemantsain.com

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Nested loop R code

2017-09-08 Thread Ulrik Stervbo
Hi Hemant,

please write to the r-help list in the future.

Look at the cut () function to solve your problem.

Also, you have a problem in your example - 5 is placed in two different
categories.

HTH
Ulrik

On Fri, 8 Sep 2017 at 12:16 Hemant Sain  wrote:

> i have a vector containing values ranging from 0 to 24
> i want to create another variable which can categorize those values  like
> this
> please help me with an R code
>
> Thanks
>
> *Value   New_Var*10 -5
> 30 -5
> 50 -5
> 96-10
> 76-10
> 56-10
> 4 0-5
> 11  11-15
> 12 11-15
> 18  16-20
> 23  21 -25
>
>
> --
> hemantsain.com
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Nested loop and output help

2013-02-08 Thread PIKAL Petr
Hi
You must use indexing based on loops

sim(ss[i],cc[j],oo1[k],oo2[l])

an of course assign result to some object.

result[...] - sim(ss[i],cc[j],oo1[k],oo2[l])

where ... means some suitable index either chosen from loop index or 
constructed during loop execution.
Maybe it is time to go through R intro document (chapter 2 and chapter 9).

You probably could find answer yourself quicker then waiting for me to answer 
it.

Regards
Petr


From: staysafe23 [mailto:staysaf...@gmail.com]
Sent: Thursday, February 07, 2013 9:09 PM
To: PIKAL Petr
Subject: RE: [R] Nested loop and output help


Hi Petr,

I have refined my question and would like to ask it another way if I may. Here 
are the conditions I would like to vary in my inquiry.

ss - seq(5,9,by=1) 5 6 7 8 9

cc - seq(-0.5,0.5, by=0.25) -0.50 -0.25 0.00 0.25 0.50

oo1 - seq(-10,10, by=5) -10 -5 0 5 10

oo2 - seq(0,20, by=5) 0 5 10 15 20

Here is the loop I would like to run.

for(i in ss) {

for (j in cc) {

for (k in oo1) {

for (l in oo2) {

sim(ss,cc,oo1,oo2)





}

}

}

}

There are the results that would like to get.

sim(5,-0.5,-10,0)

sim(5,-0.5,-10,5)

sim(5,-0.5,-10,10)

sim(5,-0.5,-10,15)

sim(5,-0.5,-10,20)

. . .

. . .

. . .

sim(9,0.5,10,0)

sim(9,0.5,10,5)

sim(9,0.5,10,10)

sim(9,0.5,10,15)

sim(9,0.5,10,20)

Here is my function but I am not getting the loop to run over all the possible 
permutations of the conditions nor and I getting an output for each condition.

sim - function(ss,cc,oo1,oo2) {

lll - vector(mode = list, length = 16)

names(lll) - c(og_table, McNemar, McNemar_corr,

sd_table, Chisq, Chisq_corr,

og_data, mean_X, cut_T1, mean_X2,

mean_Y,cut_T2, mean_Y2,

dev_data, sum_D, sum_D2)

z1 - rnorm(ss,mean=400, sd=70)

z2 - rnorm(ss,mean=450, sd=90)

r - cc

X - z1

Y = r*z1+(1-r)*z2

lll[[og_data]] - cbind(X,Y)

dev1 - oo1

lll[[cut_T1]] - mean(X) + dev1

dev2 - oo2

lll[[cut_T2]] - mean(X) + dev1 + dev2

X2 - X-lll[[cut_T1]]

Y2 - Y-lll[[cut_T2]]

c3 - cor(X2,Y2)

D - Y2-X2

lll[[sum_D]] - sum(D)

D2 -D*D

lll[[sum_D2]] - sum(D2)

lll[[dev_data]] -cbind(X2,Y2)

a11 - ifelse( X  lll[[cut_T1]]  Y  lll[[cut_T2]], 1, 0)

a12 - ifelse( X  lll[[cut_T1]]  Y = lll[[cut_T2]], 1, 0)

a21 - ifelse( X = lll[[cut_T1]]  Y  lll[[cut_T2]], 1, 0)

a22 - ifelse( X = lll[[cut_T1]]  Y = lll[[cut_T2]], 1, 0)

lll[[og_table]] -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

lll[[McNemar]] -mcnemar.test(lll[[og_table]], correct=FALSE)

lll[[McNemar_corr]] -mcnemar.test(lll[[og_table]], correct=TRUE)

lll[[sd_table]] -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

lll[[Chisq]] -chisq.test(lll[[sd_table]], correct = FALSE)

lll[[Chisq_corr]] -chisq.test(lll[[sd_table]], correct = TRUE)

lll[[mean_X]] - mean(X)

lll[[mean_Y]] - mean(Y)

lll[[mean_X2]] - mean(X2)

lll[[mean_Y2]] - mean(Y2)

lll[[T]] - sum(D)/((length(D)*sum(D2)-sum(D)*sum(D))/(length(D)-1))**(0.5)

lll[[dep_t]] - t.test(Y2, X2, alternative='two.sided', conf.level=0.95, 
paired=TRUE)

print(lll)

capture.output(print(lll), file = 
C:/Chi_Square_fix/temp_[ss]_[cc]_[oo1]_[002].txt, append = TRUE)

}
On Feb 6, 2013 3:26 PM, staysafe23 
staysaf...@gmail.commailto:staysaf...@gmail.com wrote:

Hi Petr,

Thank you again for your help. I am making a mistake since I have assigned an 
entire vector oo1 to dev1. What I errantly thought I was doing was starting 
with the value of 5 in ss, then picking the value of -0.50 in cc, then picking 
the value of -10 in oo1, then picking the value of 0 in oo2, and running the 
code that is within the inner most brackets and putting the output into an 
output file.

Then I thought the loop would move on to the next case, 5 in ss, then picking 
the value of -0.50 in cc, then picking the value of -10 in oo1, then picking 
the value of 5 in oo2. So I thought I would end up with my code being run a 
number of times that would equal the 
length(ss)*length(cc)*length(oo1)*length(oo2) .

I can't seem to get the code to run through each of these conditions. Do you 
have any idea that may help me do so?

Thank you Petr,

Thomas
On Feb 6, 2013 2:25 AM, PIKAL Petr 
petr.pi...@precheza.czmailto:petr.pi...@precheza.cz wrote:
Hi

This is an error I get with your loop code due to this line

capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append = TRUE)
Error in file(file, if (append) a else w) :
  cannot open the connection
In addition: Warning messages:
1: In chisq.test(lll[[mat4]], correct = FALSE) :
  Chi-squared approximation may be incorrect
2: In chisq.test(lll[[mat4]], correct = TRUE) :
  Chi-squared approximation may be incorrect
3: In file(file, if (append) a else w) :
  cannot open file 'C:/Chi_Square_fix/temp.txt': No such file or directory


Without this line I get no error and everything is probably printed. I do not 
know function capture.output but I presume that you need initialise the file 
temp.txt before you can output to it.

Anyway those cycles are rather weird. E.g. in each cycle you assign whole 
vector oo1

Re: [R] Nested loop and output help

2013-02-06 Thread PIKAL Petr
Hi

This is an error I get with your loop code due to this line

capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append = TRUE)
Error in file(file, if (append) a else w) :
  cannot open the connection
In addition: Warning messages:
1: In chisq.test(lll[[mat4]], correct = FALSE) :
  Chi-squared approximation may be incorrect
2: In chisq.test(lll[[mat4]], correct = TRUE) :
  Chi-squared approximation may be incorrect
3: In file(file, if (append) a else w) :
  cannot open file 'C:/Chi_Square_fix/temp.txt': No such file or directory


Without this line I get no error and everything is probably printed. I do not 
know function capture.output but I presume that you need initialise the file 
temp.txt before you can output to it.

Anyway those cycles are rather weird. E.g. in each cycle you assign whole 
vector oo1 to dev1.
Is it intended?

Regards
Petr


From: staysafe23 [mailto:staysaf...@gmail.com]
Sent: Friday, February 01, 2013 10:12 PM
To: PIKAL Petr
Cc: r-help@r-project.org
Subject: RE: [R] Nested loop and output help


Thank you very much Petr,

I believe I have fixed my inquiry to not use floating points in my cycle as you 
pointed out and to use the list structure to keep my results. I am still at a 
loss as to how to run the multiple loops. I have tried quite a few different 
strategies but my failure seems to illustrate that my understanding of how the 
loops will run is nonexistent.

I would like to simultaneously let the following 4 things vary:

z1 - rnorm(ss,mean=400, sd=70) and z2 - rnorm(ss,mean=450, sd=90) by ss - 
seq(5,9,by=1) which yields 5 6 7 8 9

r - cc by cc - seq(-0.5,0.5, by=0.25) which yields -0.50 -0.25 0.00 0.25 0.50

dev1 - oo1 by oo1 - seq(-10,10, by=5) which yields -10 -5 0 5 10

dev2 - oo2 by oo2 - seq(0,20, by=5) which yields 0 5 10 15 20

I tried to run the loops that would vary each of these above conditions with 
the looped code attached below and failed very badly.

Thank you Petr and all,

Best,

Thomas

###SINGLE RUN 
CODE

lll - vector(mode = list, length = 10)

names(lll) - c(mat1, mat2, mat3, mat4, cut1, cut2, out3a, 
out3b, out4a, out4b)

z1 - rnorm(20,mean=400, sd=70)

z2 - rnorm(20,mean=450, sd=90)

cor - runif(1,min=0.4, max=0.6)

X - z1

Y = cor*z1+(1-cor)*z2

lll[[mat1]] - cbind(X,Y)

dev1 - sample(-40:40, 1, replace=T)

lll[[cut1]] - mean(X) + dev1

dev2 - sample(12:54, 1, replace=T)

lll[[cut2]] - mean(X) + dev1 + dev2

X2 - X-lll[[cut1]]

Y2 - Y-lll[[cut2]]

c3 - cor(X2,Y2)

lll[[mat2]] -cbind(X2,Y2)

a11 - ifelse( X  lll[[cut1]]  Y  lll[[cut2]], 1, 0)

a12 - ifelse( X  lll[[cut1]]  Y = lll[[cut2]], 1, 0)

a21 - ifelse( X = lll[[cut1]]  Y  lll[[cut2]], 1, 0)

a22 - ifelse( X = lll[[cut1]]  Y = lll[[cut2]], 1, 0)

lll[[mat3]] -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

lll[[mat4]] -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

lll[[out3a]] - mcnemar.test(lll[[mat3]], correct=FALSE)

lll[[out3b]] - mcnemar.test(lll[[mat3]], correct=TRUE)

lll[[out4a]] - chisq.test(lll[[mat4]], correct = FALSE)

lll[[out4b]] - chisq.test(lll[[mat4]], correct = TRUE)

print(lll)

capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append = TRUE)

##LOOPED 
CODE#

lll - vector(mode = list, length = 10)

names(lll) - c(mat1, mat2, mat3, mat4, cut1, cut2, out3a, 
out3b, out4a, out4b)

ss - seq(5,9,by=1)

cc - seq(-0.5,0.5, by=0.25)

oo1 - seq(-10,10, by=5)

oo2 - seq(0,20, by=5)

for(i in ss) {

for (j in cc) {

for (k in oo1) {

for (l in oo2) {

z1 - rnorm(ss,mean=400, sd=70)

z2 - rnorm(ss,mean=450, sd=90)

r - cc

X - z1

Y = r*z1+(1-r)*z2

lll[[mat1]] - cbind(X,Y)

dev1 - oo1

lll[[cut1]] - mean(X) + dev1

dev2 - oo2

lll[[cut2]] - mean(X) + dev1 + dev2

X2 - X-lll[[cut1]]

Y2 - Y-lll[[cut2]]

c3 - cor(X2,Y2)

lll[[mat2]] -cbind(X2,Y2)

a11 - ifelse( X  lll[[cut1]]  Y  lll[[cut2]], 1, 0)

a12 - ifelse( X  lll[[cut1]]  Y = lll[[cut2]], 1, 0)

a21 - ifelse( X = lll[[cut1]]  Y  lll[[cut2]], 1, 0)

a22 - ifelse( X = lll[[cut1]]  Y = lll[[cut2]], 1, 0)

lll[[mat3]] -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

lll[[mat4]] -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

lll[[out3a]] - mcnemar.test(lll[[mat3]], correct=FALSE)

lll[[out3b]] - mcnemar.test(lll[[mat3]], correct=TRUE)

lll[[out4a]] - chisq.test(lll[[mat4]], correct = FALSE)

lll[[out4b]] - chisq.test(lll[[mat4]], correct = TRUE)

print(lll)

capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append = TRUE)

}

}

}

}
On Feb 1, 2013 2:01 AM, PIKAL Petr 
petr.pi...@precheza.czmailto:petr.pi...@precheza.cz wrote:
Hi

see inline

 -Original Message-
 From: r-help-boun...@r-project.orgmailto:r-help-boun...@r-project.org 
 [mailto:r-help-bounces@r-mailto:r-help-bounces@r-
 project.orghttp://project.org] On Behalf Of staysafe23
 Sent: Friday, February 01, 2013 1:01 AM
 To: r-help@r

Re: [R] Nested loop and output help

2013-02-02 Thread staysafe23
Dear R help moderator,

My name is Thomas and I am new here. I am wondering if my post violated
some of the rules of posting here. If so can you please let me know what I
have done so that I can repost? I reread the posting guidelines and don't
see anything that I violate. I have been reading a great deal online but
just can figure out how to run the loop correctly.

Thank you kindly,

Tom
On Feb 1, 2013 2:12 PM, staysafe23 staysaf...@gmail.com wrote:

 Thank you very much Petr,

 I believe I have fixed my inquiry to not use floating points in my cycle
 as you pointed out and to use the list structure to keep my results. I am
 still at a loss as to how to run the multiple loops. I have tried quite a
 few different strategies but my failure seems to illustrate that my
 understanding of how the loops will run is nonexistent.

 I would like to simultaneously let the following 4 things vary:

 z1 - rnorm(ss,mean=400, sd=70) and z2 - rnorm(ss,mean=450, sd=90) by ss
 - seq(5,9,by=1) which yields 5 6 7 8 9

 r - cc by cc - seq(-0.5,0.5, by=0.25) which yields -0.50 -0.25 0.00 0.25
 0.50

 dev1 - oo1 by oo1 - seq(-10,10, by=5) which yields -10 -5 0 5 10

 dev2 - oo2 by oo2 - seq(0,20, by=5) which yields 0 5 10 15 20

 I tried to run the loops that would vary each of these above conditions
 with the looped code attached below and failed very badly.

 Thank you Petr and all,

 Best,

 Thomas

 ###SINGLE RUN
 CODE

 lll - vector(mode = list, length = 10)

 names(lll) - c(mat1, mat2, mat3, mat4, cut1, cut2, out3a,
 out3b, out4a, out4b)

 z1 - rnorm(20,mean=400, sd=70)

 z2 - rnorm(20,mean=450, sd=90)

 cor - runif(1,min=0.4, max=0.6)

 X - z1

 Y = cor*z1+(1-cor)*z2

 lll[[mat1]] - cbind(X,Y)

 dev1 - sample(-40:40, 1, replace=T)

 lll[[cut1]] - mean(X) + dev1

 dev2 - sample(12:54, 1, replace=T)

 lll[[cut2]] - mean(X) + dev1 + dev2

 X2 - X-lll[[cut1]]

 Y2 - Y-lll[[cut2]]

 c3 - cor(X2,Y2)

 lll[[mat2]] -cbind(X2,Y2)

 a11 - ifelse( X  lll[[cut1]]  Y  lll[[cut2]], 1, 0)

 a12 - ifelse( X  lll[[cut1]]  Y = lll[[cut2]], 1, 0)

 a21 - ifelse( X = lll[[cut1]]  Y  lll[[cut2]], 1, 0)

 a22 - ifelse( X = lll[[cut1]]  Y = lll[[cut2]], 1, 0)

 lll[[mat3]] -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

 lll[[mat4]] -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

 lll[[out3a]] - mcnemar.test(lll[[mat3]], correct=FALSE)

 lll[[out3b]] - mcnemar.test(lll[[mat3]], correct=TRUE)

 lll[[out4a]] - chisq.test(lll[[mat4]], correct = FALSE)

 lll[[out4b]] - chisq.test(lll[[mat4]], correct = TRUE)

 print(lll)

 capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append =
 TRUE)

 ##LOOPED
 CODE#

 lll - vector(mode = list, length = 10)

 names(lll) - c(mat1, mat2, mat3, mat4, cut1, cut2, out3a,
 out3b, out4a, out4b)

 ss - seq(5,9,by=1)

 cc - seq(-0.5,0.5, by=0.25)

 oo1 - seq(-10,10, by=5)

 oo2 - seq(0,20, by=5)

 for(i in ss) {

 for (j in cc) {

 for (k in oo1) {

 for (l in oo2) {

 z1 - rnorm(ss,mean=400, sd=70)

 z2 - rnorm(ss,mean=450, sd=90)

 r - cc

 X - z1

 Y = r*z1+(1-r)*z2

 lll[[mat1]] - cbind(X,Y)

 dev1 - oo1

 lll[[cut1]] - mean(X) + dev1

 dev2 - oo2

 lll[[cut2]] - mean(X) + dev1 + dev2

 X2 - X-lll[[cut1]]

 Y2 - Y-lll[[cut2]]

 c3 - cor(X2,Y2)

 lll[[mat2]] -cbind(X2,Y2)

 a11 - ifelse( X  lll[[cut1]]  Y  lll[[cut2]], 1, 0)

 a12 - ifelse( X  lll[[cut1]]  Y = lll[[cut2]], 1, 0)

 a21 - ifelse( X = lll[[cut1]]  Y  lll[[cut2]], 1, 0)

 a22 - ifelse( X = lll[[cut1]]  Y = lll[[cut2]], 1, 0)

 lll[[mat3]] -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

 lll[[mat4]] -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

 lll[[out3a]] - mcnemar.test(lll[[mat3]], correct=FALSE)

 lll[[out3b]] - mcnemar.test(lll[[mat3]], correct=TRUE)

 lll[[out4a]] - chisq.test(lll[[mat4]], correct = FALSE)

 lll[[out4b]] - chisq.test(lll[[mat4]], correct = TRUE)

 print(lll)

 capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append =
 TRUE)

 }

 }

 }

 }
 On Feb 1, 2013 2:01 AM, PIKAL Petr petr.pi...@precheza.cz wrote:

 Hi

 see inline

  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org] On Behalf Of staysafe23
  Sent: Friday, February 01, 2013 1:01 AM
  To: r-help@r-project.org
  Subject: [R] Nested loop and output help
 
  Hello Everyone,
 
  My name is Thomas and I have been using R for one week. I recently
  found your site and have been able to search the archives of posts.
  This has given me some great information that has allowed me to craft
  an initial design to an inquiry I would like to make into the breakdown
  of McNemar's test. I have read an intro to R manual and the posting
  guides and hope I am not violating them with this post. If so I will
  re-ask my question in the proper format.
 
  I have succeeded in writing a loop to vary one condition of my inquiry

Re: [R] Nested loop and output help

2013-02-01 Thread PIKAL Petr
Hi

see inline

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of staysafe23
 Sent: Friday, February 01, 2013 1:01 AM
 To: r-help@r-project.org
 Subject: [R] Nested loop and output help
 
 Hello Everyone,
 
 My name is Thomas and I have been using R for one week. I recently
 found your site and have been able to search the archives of posts.
 This has given me some great information that has allowed me to craft
 an initial design to an inquiry I would like to make into the breakdown
 of McNemar's test. I have read an intro to R manual and the posting
 guides and hope I am not violating them with this post. If so I will
 re-ask my question in the proper format.
 
 I have succeeded in writing a loop to vary one condition of my inquiry
 but I am unable to understand how to vary the remaining three
 conditions, each with its own for loop. Each time I try to do so I fail
 miserably. Here is my current code :
 
 n - seq(5,10,by=1)
 
 for(i in n) {
 
 z1 - rnorm(n,mean=400, sd=70)
 
 z2 - rnorm(n,mean=450, sd=90)
 
 cor - runif(1,min=0.4, max=0.6)
 
 X - z1
 
 Y = cor*z1+(1-cor)*z2
 
 mat1 - cbind(X,Y)
 
 dev1 - sample(-40:40, 1, replace=T)
 
 cut1 - mean(X) + dev1
 
 dev2 - sample(12:54, 1, replace=T)
 
 cut2 - mean(X) + dev1 + dev2
 
 X2 - X-cut1
 
 Y2 - Y-cut2
 
 c3 - cor(X2,Y2)
 
 mat2 -cbind(X2,Y2)
 
 a11 - ifelse( X  cut1  Y  cut2, 1, 0)
 
 a12 - ifelse( X  cut1  Y = cut2, 1, 0)
 
 a21 - ifelse( X = cut1  Y  cut2, 1, 0)
 
 a22 - ifelse( X = cut1  Y = cut2, 1, 0)
 
 mat3 -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)
 
 mat4 -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)
 
 out3a - mcnemar.test(mat3, correct=FALSE)
 
 out3b - mcnemar.test(mat3, correct=TRUE)
 
 out4a - chisq.test(mat4, correct = FALSE)
 
 out4b - chisq.test(mat4, correct = TRUE)
 
 print(mat1)
 
 print(mat2)
 
 print(cut1)
 
 print(cut2)
 
 print(mat3)
 
 print(out3a)
 
 print(out3b)
 
 print(mat4)
 
 print(out4a)
 
 print(out4b)
 
 }
 
 .

Use list structure for keeping such results.

lll-list()
for(j in 1:5) {
lll[[j]] - list()
for( i in 1:3) lll[[j]][[i]]-rnorm(10)
}

after population of a list you can print it as a whole or only part. Here is an 
example with your code.

n - seq(5,10,by=1)

lll - vector(mode = list, length = 10)
names(lll) - c(mat1, mat2, mat3, mat4, cut1, cut2, out3a, 
out3b, out4a, out4b)

n - seq(5,10,by=1)

for(i in n) {
z1 - rnorm(n,mean=400, sd=70)
z2 - rnorm(n,mean=450, sd=90)
cor - runif(1,min=0.4, max=0.6)
X - z1
Y = cor*z1+(1-cor)*z2

lll[[mat1]] - cbind(X,Y)
dev1 - sample(-40:40, 1, replace=T)

lll[[cut1]] - mean(X) + dev1
dev2 - sample(12:54, 1, replace=T)

lll[[cut2]] - mean(X) + dev1 + dev2
X2 - X-cut1
Y2 - Y-cut2
c3 - cor(X2,Y2)

lll[[mat2]] -cbind(X2,Y2)
a11 - ifelse( X  cut1  Y  cut2, 1, 0)
a12 - ifelse( X  cut1  Y = cut2, 1, 0)
a21 - ifelse( X = cut1  Y  cut2, 1, 0)
a22 - ifelse( X = cut1  Y = cut2, 1, 0)

lll[[mat3]] -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)
lll[[mat4]] -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)
lll[[out3a]] - mcnemar.test(mat3, correct=FALSE)
lll[[out3b]] - mcnemar.test(mat3, correct=TRUE)
lll[[out4a]] - chisq.test(mat4, correct = FALSE)
lll[[out4b]] - chisq.test(mat4, correct = TRUE)
}

print(lll)

 
 Other than the sample size of the random draws I would like to nest for
 loops for cor, dev1, and dev2 according to the following sequences
 respectively:
 
 cor - seq(-0.5,0.5, by=0.25)

do not use floating points in cycle.

better to use

for (k in 1:n) {
cc - cor[k]
make computation with cc
}


 
 dev1 - seq(-100,100, by=10)
 
 dev2 - seq(12,54, by=10)
 
 .
 
 After doing so I would like to put each matrix and their respective
 tests into a text file so that I can examine the results. I would like
 to put the results in a .txt file each time the loop finishes one case.
 I would like to append this text file with subsequent matrices and
 results rendered by each iteration of the nested for loop.  I have seen
 some very nice examples of output that R can render. I would like to
 simply display each matrix and their tests.

maybe R2HTML or latex in Hmisc package can 

Regards 
Petr

 
 Thank you to all the teachers and students on this forum. The only
 reason I have been able to craft this inquiry is due to the questions
 and answers I have found through searching the archive. Thank you
 kindly for your assistance and for freely sharing your knowledge.
 
 Best wishes,
 Thomas
 
   [[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

Re: [R] Nested loop and output help

2013-02-01 Thread staysafe23
Thank you very much Petr,

I believe I have fixed my inquiry to not use floating points in my cycle as
you pointed out and to use the list structure to keep my results. I am
still at a loss as to how to run the multiple loops. I have tried quite a
few different strategies but my failure seems to illustrate that my
understanding of how the loops will run is nonexistent.

I would like to simultaneously let the following 4 things vary:

z1 - rnorm(ss,mean=400, sd=70) and z2 - rnorm(ss,mean=450, sd=90) by ss
- seq(5,9,by=1) which yields 5 6 7 8 9

r - cc by cc - seq(-0.5,0.5, by=0.25) which yields -0.50 -0.25 0.00 0.25
0.50

dev1 - oo1 by oo1 - seq(-10,10, by=5) which yields -10 -5 0 5 10

dev2 - oo2 by oo2 - seq(0,20, by=5) which yields 0 5 10 15 20

I tried to run the loops that would vary each of these above conditions
with the looped code attached below and failed very badly.

Thank you Petr and all,

Best,

Thomas

###SINGLE RUN
CODE

lll - vector(mode = list, length = 10)

names(lll) - c(mat1, mat2, mat3, mat4, cut1, cut2, out3a,
out3b, out4a, out4b)

z1 - rnorm(20,mean=400, sd=70)

z2 - rnorm(20,mean=450, sd=90)

cor - runif(1,min=0.4, max=0.6)

X - z1

Y = cor*z1+(1-cor)*z2

lll[[mat1]] - cbind(X,Y)

dev1 - sample(-40:40, 1, replace=T)

lll[[cut1]] - mean(X) + dev1

dev2 - sample(12:54, 1, replace=T)

lll[[cut2]] - mean(X) + dev1 + dev2

X2 - X-lll[[cut1]]

Y2 - Y-lll[[cut2]]

c3 - cor(X2,Y2)

lll[[mat2]] -cbind(X2,Y2)

a11 - ifelse( X  lll[[cut1]]  Y  lll[[cut2]], 1, 0)

a12 - ifelse( X  lll[[cut1]]  Y = lll[[cut2]], 1, 0)

a21 - ifelse( X = lll[[cut1]]  Y  lll[[cut2]], 1, 0)

a22 - ifelse( X = lll[[cut1]]  Y = lll[[cut2]], 1, 0)

lll[[mat3]] -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

lll[[mat4]] -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

lll[[out3a]] - mcnemar.test(lll[[mat3]], correct=FALSE)

lll[[out3b]] - mcnemar.test(lll[[mat3]], correct=TRUE)

lll[[out4a]] - chisq.test(lll[[mat4]], correct = FALSE)

lll[[out4b]] - chisq.test(lll[[mat4]], correct = TRUE)

print(lll)

capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append =
TRUE)

##LOOPED
CODE#

lll - vector(mode = list, length = 10)

names(lll) - c(mat1, mat2, mat3, mat4, cut1, cut2, out3a,
out3b, out4a, out4b)

ss - seq(5,9,by=1)

cc - seq(-0.5,0.5, by=0.25)

oo1 - seq(-10,10, by=5)

oo2 - seq(0,20, by=5)

for(i in ss) {

for (j in cc) {

for (k in oo1) {

for (l in oo2) {

z1 - rnorm(ss,mean=400, sd=70)

z2 - rnorm(ss,mean=450, sd=90)

r - cc

X - z1

Y = r*z1+(1-r)*z2

lll[[mat1]] - cbind(X,Y)

dev1 - oo1

lll[[cut1]] - mean(X) + dev1

dev2 - oo2

lll[[cut2]] - mean(X) + dev1 + dev2

X2 - X-lll[[cut1]]

Y2 - Y-lll[[cut2]]

c3 - cor(X2,Y2)

lll[[mat2]] -cbind(X2,Y2)

a11 - ifelse( X  lll[[cut1]]  Y  lll[[cut2]], 1, 0)

a12 - ifelse( X  lll[[cut1]]  Y = lll[[cut2]], 1, 0)

a21 - ifelse( X = lll[[cut1]]  Y  lll[[cut2]], 1, 0)

a22 - ifelse( X = lll[[cut1]]  Y = lll[[cut2]], 1, 0)

lll[[mat3]] -matrix(c(sum(a11),sum(a21), sum(a12),sum(a22)), nrow = 2)

lll[[mat4]] -matrix(c(sum(a11),sum(a22), sum(a12),sum(a21)), nrow = 2)

lll[[out3a]] - mcnemar.test(lll[[mat3]], correct=FALSE)

lll[[out3b]] - mcnemar.test(lll[[mat3]], correct=TRUE)

lll[[out4a]] - chisq.test(lll[[mat4]], correct = FALSE)

lll[[out4b]] - chisq.test(lll[[mat4]], correct = TRUE)

print(lll)

capture.output(print(lll), file = C:/Chi_Square_fix/temp.txt, append =
TRUE)

}

}

}

}
On Feb 1, 2013 2:01 AM, PIKAL Petr petr.pi...@precheza.cz wrote:

 Hi

 see inline

  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org] On Behalf Of staysafe23
  Sent: Friday, February 01, 2013 1:01 AM
  To: r-help@r-project.org
  Subject: [R] Nested loop and output help
 
  Hello Everyone,
 
  My name is Thomas and I have been using R for one week. I recently
  found your site and have been able to search the archives of posts.
  This has given me some great information that has allowed me to craft
  an initial design to an inquiry I would like to make into the breakdown
  of McNemar's test. I have read an intro to R manual and the posting
  guides and hope I am not violating them with this post. If so I will
  re-ask my question in the proper format.
 
  I have succeeded in writing a loop to vary one condition of my inquiry
  but I am unable to understand how to vary the remaining three
  conditions, each with its own for loop. Each time I try to do so I fail
  miserably. Here is my current code :
 
  n - seq(5,10,by=1)
 
  for(i in n) {
 
  z1 - rnorm(n,mean=400, sd=70)
 
  z2 - rnorm(n,mean=450, sd=90)
 
  cor - runif(1,min=0.4, max=0.6)
 
  X - z1
 
  Y = cor*z1+(1-cor)*z2
 
  mat1 - cbind(X,Y)
 
  dev1 - sample(-40:40, 1, replace=T)
 
  cut1 - mean(X) + dev1
 
  dev2 - sample(12:54, 1, replace=T)
 
  cut2 - mean(X) + dev1 + dev2
 
  X2 - X-cut1
 
  Y2 - Y-cut2

Re: [R] nested loop for

2011-07-22 Thread paloma ruiz benito

Dear Dennins,

Many thanks for you reply. Sorry for the convoluted loops, but I am starting to 
learn some programming and the loops are a bit complicated for me. Finally, a 
collegue help me with the code, and one code that works is like follow:

Pma-rep (1:40)
P-seq(1,4, 1)
Plot-rep(P,10)
dbh2-rnorm(40, mean=200, sd=5)
SBA2-rnorm(40, mean=10, sd=1)

BAL2-rep(0,length(Pma))

data-data.frame(Pma,Plot,dbh2,SBA2,BAL2)

for (p in 1:length(unique(Plot))){ 
index.stand-which(data$Plot==p)  
data.aux-data[ index.stand , ] 
result.aux-numeric() 
for (i in 1:length(data.aux$Pma)){
result.aux[i] -sum(data.aux$SBA2[which(data.aux$dbh2 data.aux[i , 
]$dbh2)]) }
data$BAL2[ index.stand]-result.aux 
rm(data.aux)
rm(result.aux) 
}
data

Best,

Paloma

 Date: Thu, 21 Jul 2011 14:18:37 -0700
 Subject: Re: [R] nested loop for
 From: djmu...@gmail.com
 To: paloma_...@hotmail.com
 CC: r-help@r-project.org
 
 Hi:
 
 I *think* this is what you're after, but I get dizzy trying to read
 convoluted loops. Try this instead;
 
 # Function to find the total SBA2 for all dbh2 larger than the given one:
 foo - function(d) {
 d - d[order(d$dbh2), ]
 d - transform(d, BAL2 = sum(SBA2) - cumsum(SBA2))
 d
   }
 
 library('plyr')
 ddply(kk, .(Plot), foo)
Pma Plot dbh2  SBA2  BAL2
 1   291 185.8568  9.055821 91.530165
 2   331 186.4623 10.762347 80.767818
 311 192.8324 10.741988 70.025830
 4   171 196.2093  9.484601 60.541229
 5   211 204.0971 11.389817 49.151412
 6   131 204.5070  9.644655 39.506756
 791 205.3079 11.014892 28.491864
 8   251 206.5908 10.041878 18.449986
 951 206.8110  8.602678  9.847307
 10  371 211.1735  9.847307  0.00
 ...
 
 # If you want the groupwise sum in the output, use the following
 version instead:
 foo - function(d) {
 d - d[order(d$dbh2), ]
 d - transform(d, BAL2 = sum(SBA2) - cumsum(SBA2),
  aa = sum(SBA2))
 d
   }
 
 HTH,
 Dennis
 
 On Thu, Jul 21, 2011 at 4:13 AM, paloma ruiz benito
 paloma_...@hotmail.com wrote:
 
  Hi everyone,
 
  I have been working some days in a nested loop in R but I can't find the 
  solution.
 
  I have a data.frame with an unique ID for individuals and unique ID for 
  different stands, for each indiviadual I have a dbh record and a SBA (stand 
  basal area) field.
 
  Pma-rep (1:40)
  P-seq(1,4, 1)
  Plot-rep(P,10)
  dbh2-rnorm(40, mean=200, sd=5)
  SBA2-rnorm(40, mean=10, sd=1)
 
  As I want to calculate the basal area of larger trees in each stand (i.e., 
  the compare tree to tree the dbh and for each individual sum the stand 
  basal area of larger trees)
 
  BAL2-rep(0,length(Pma))
  aa-rep(0,length(Pma))
 
  Now I have programed a for loop to do this calculation, and one plot it 
  works quite well:
 
  kk-data.frame(Pma, Plot, dbh2, SBA2, BAL2, aa)
  kkk-kk[kk$Plot==1,]
 
 
  The loop:
 
 for(j in 1:length(kkk$Pma)){
 for(i in 1:length(kkk$Pma)){
 if(kkk$dbh2[j]kkk$dbh2[i])
 kkk$aa[i]-kkk$SBA2[i]
 else temp_data$aa[i]-0
 kkk$BAL2[j]-sum(kkk$aa)
 }
 }
 
  But, I have tried a million of forms to calculate this for each stand... 
  and no one looks fine. The closest code that I have got is:
 
  for(k in 1:length(kk)){
 temp_data-kk[kk$Plot==Plot[k],]
 
 for(j in 1:length(temp_data$Pma)){
 #I have selected the individuals to calculate the BAL in each plot
 for(i in 1:length(temp_data$Pma)){
 if(temp_data$dbh2[j]temp_data$dbh2[i])
 temp_data$aa[i]-temp_data$SBA2[i]
 else temp_data$aa[i]-0
 temp_data$BAL2[j]-sum(temp_data$aa)
 
 
 }
 
 ###Some suggestion to save the temporal data of each stand and group 
  it together
 }
 
  }
 
  Any advise or suggestion will be very welcome,
 
  Thanks in advance,
 
  Paloma
 
 
 [[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.
 
  
[[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] nested loop for

2011-07-21 Thread paloma ruiz benito

Hi everyone,

I have been working some days in a nested loop in R but I can't find the 
solution. 

I have a data.frame with an unique ID for individuals and unique ID for 
different stands, for each indiviadual I have a dbh record and a SBA (stand 
basal area) field.

Pma-rep (1:40)
P-seq(1,4, 1)
Plot-rep(P,10)
dbh2-rnorm(40, mean=200, sd=5)
SBA2-rnorm(40, mean=10, sd=1)

As I want to calculate the basal area of larger trees in each stand (i.e., the 
compare tree to tree the dbh and for each individual sum the stand basal area 
of larger trees)

BAL2-rep(0,length(Pma))
aa-rep(0,length(Pma))

Now I have programed a for loop to do this calculation, and one plot it works 
quite well:

kk-data.frame(Pma, Plot, dbh2, SBA2, BAL2, aa)
kkk-kk[kk$Plot==1,]


The loop:

for(j in 1:length(kkk$Pma)){
for(i in 1:length(kkk$Pma)){
if(kkk$dbh2[j]kkk$dbh2[i])
kkk$aa[i]-kkk$SBA2[i]
else temp_data$aa[i]-0
kkk$BAL2[j]-sum(kkk$aa)
}
}

But, I have tried a million of forms to calculate this for each stand... and no 
one looks fine. The closest code that I have got is:

for(k in 1:length(kk)){
temp_data-kk[kk$Plot==Plot[k],]

for(j in 1:length(temp_data$Pma)){
#I have selected the individuals to calculate the BAL in each plot
for(i in 1:length(temp_data$Pma)){
if(temp_data$dbh2[j]temp_data$dbh2[i])
temp_data$aa[i]-temp_data$SBA2[i]
else temp_data$aa[i]-0
temp_data$BAL2[j]-sum(temp_data$aa)


}

###Some suggestion to save the temporal data of each stand and group it 
together
}

}

Any advise or suggestion will be very welcome,

Thanks in advance,

Paloma

  
[[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] nested loop for

2011-07-21 Thread Dennis Murphy
Hi:

I *think* this is what you're after, but I get dizzy trying to read
convoluted loops. Try this instead;

# Function to find the total SBA2 for all dbh2 larger than the given one:
foo - function(d) {
d - d[order(d$dbh2), ]
d - transform(d, BAL2 = sum(SBA2) - cumsum(SBA2))
d
  }

library('plyr')
ddply(kk, .(Plot), foo)
   Pma Plot dbh2  SBA2  BAL2
1   291 185.8568  9.055821 91.530165
2   331 186.4623 10.762347 80.767818
311 192.8324 10.741988 70.025830
4   171 196.2093  9.484601 60.541229
5   211 204.0971 11.389817 49.151412
6   131 204.5070  9.644655 39.506756
791 205.3079 11.014892 28.491864
8   251 206.5908 10.041878 18.449986
951 206.8110  8.602678  9.847307
10  371 211.1735  9.847307  0.00
...

# If you want the groupwise sum in the output, use the following
version instead:
foo - function(d) {
d - d[order(d$dbh2), ]
d - transform(d, BAL2 = sum(SBA2) - cumsum(SBA2),
 aa = sum(SBA2))
d
  }

HTH,
Dennis

On Thu, Jul 21, 2011 at 4:13 AM, paloma ruiz benito
paloma_...@hotmail.com wrote:

 Hi everyone,

 I have been working some days in a nested loop in R but I can't find the 
 solution.

 I have a data.frame with an unique ID for individuals and unique ID for 
 different stands, for each indiviadual I have a dbh record and a SBA (stand 
 basal area) field.

 Pma-rep (1:40)
 P-seq(1,4, 1)
 Plot-rep(P,10)
 dbh2-rnorm(40, mean=200, sd=5)
 SBA2-rnorm(40, mean=10, sd=1)

 As I want to calculate the basal area of larger trees in each stand (i.e., 
 the compare tree to tree the dbh and for each individual sum the stand basal 
 area of larger trees)

 BAL2-rep(0,length(Pma))
 aa-rep(0,length(Pma))

 Now I have programed a for loop to do this calculation, and one plot it works 
 quite well:

 kk-data.frame(Pma, Plot, dbh2, SBA2, BAL2, aa)
 kkk-kk[kk$Plot==1,]


 The loop:

        for(j in 1:length(kkk$Pma)){
            for(i in 1:length(kkk$Pma)){
                if(kkk$dbh2[j]kkk$dbh2[i])
                kkk$aa[i]-kkk$SBA2[i]
                else temp_data$aa[i]-0
                kkk$BAL2[j]-sum(kkk$aa)
    }
    }

 But, I have tried a million of forms to calculate this for each stand... and 
 no one looks fine. The closest code that I have got is:

 for(k in 1:length(kk)){
    temp_data-kk[kk$Plot==Plot[k],]

        for(j in 1:length(temp_data$Pma)){
    #I have selected the individuals to calculate the BAL in each plot
            for(i in 1:length(temp_data$Pma)){
                if(temp_data$dbh2[j]temp_data$dbh2[i])
                temp_data$aa[i]-temp_data$SBA2[i]
                else temp_data$aa[i]-0
                temp_data$BAL2[j]-sum(temp_data$aa)


    }

    ###Some suggestion to save the temporal data of each stand and group it 
 together
    }

 }

 Any advise or suggestion will be very welcome,

 Thanks in advance,

 Paloma


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