[R] inner loop problem!?
Hi, i have 656 attributes ind INTERVALL_VAR 119 in GROUP and this morning i'm little confused why the inner loop hang if it arrive 656th column. My Task is a t-test and correlation with all columns in INTERVALL_VAR for all attributes in GROUP. many thanks regards, christian for( k in 1:length(GROUP)){ for(i in 1:length(INTERVALL_VAR)){ calc - t.test(INTERVALL_VAR[,i] ~ GROUP[,k]) korrel[i] - round(cor(INTERVALL_VAR[,i],GROUP[,k]),3) name1[i] - colnames(INTERVALL_VAR[i]) name2[i] - colnames(GROUP[k]) sig[i] - round(calc$p.value,4) estimate0[i] - round(calc$estimate[1],2) estimate1[i] - round(calc$estimate[2],2) } } result - as.data.frame(cbind(name1,name2,korrel,sig,estimate0,estimate1),row.names=F) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] inner loop problem!?
Hi, i understand my problem , because i overwrite my result's from previous loop's again and agian, really stupid :-] regards, christian Hi, i have 656 attributes ind INTERVALL_VAR 119 in GROUP and this morning i'm little confused why the inner loop hang if it arrive 656th column. My Task is a t-test and correlation with all columns in INTERVALL_VAR for all attributes in GROUP. many thanks regards, christian for( k in 1:length(GROUP)){ for(i in 1:length(INTERVALL_VAR)){ calc - t.test(INTERVALL_VAR[,i] ~ GROUP[,k]) korrel[i] - round(cor(INTERVALL_VAR[,i],GROUP[,k]),3) name1[i] - colnames(INTERVALL_VAR[i]) name2[i] - colnames(GROUP[k]) sig[i] - round(calc$p.value,4) estimate0[i] - round(calc$estimate[1],2) estimate1[i] - round(calc$estimate[2],2) } } result - as.data.frame(cbind(name1,name2,korrel,sig,estimate0,estimate1),row.names=F) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] for loop problem
aat wrote: Hello R users, A beginners question which I could not find the answer to in earler posts. My thought process: Here z is a 119 x 15 data matrix Step 1: start at column one, bind every column with column 1 Step2: use the new matrix, test, in the fitCopula package Step3: store each result in myfit, bind each result to answer Step4: return answer copula_est - function(z) { for(i in 1:length(z[1,])) { my.cop - normalCopula(param = 0.5, dim = 2) test - cbind(z[,1],z[,i]) myfit[i] - fitCopula(test,my.cop, start=0.3) } answer - cbind(myfit[i]) return(answer) } The example is not reproducible for us, since we do not have z. I'd try to rewrite it as follows, without having tried anything: my.cop - normalCopula(param = 0.5, dim = 2) answer - apply(z[,-1], 2, function(x) fitCopula(cbind(z[,1], x), my.cop, start=0.3), my.cop = my.cop) Uwe Ligges Errors received: Error: object test not found Could my syntax be incorrect, or is it a deeper faulty logic error. Thank you for your help, it is much appreciated. aat __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] for loop problem
On Sun, 21 Jan 2007, Uwe Ligges wrote: aat wrote: Hello R users, A beginners question which I could not find the answer to in earler posts. My thought process: Here z is a 119 x 15 data matrix Step 1: start at column one, bind every column with column 1 Step2: use the new matrix, test, in the fitCopula package Step3: store each result in myfit, bind each result to answer Step4: return answer copula_est - function(z) { for(i in 1:length(z[1,])) { my.cop - normalCopula(param = 0.5, dim = 2) test - cbind(z[,1],z[,i]) myfit[i] - fitCopula(test,my.cop, start=0.3) } answer - cbind(myfit[i]) return(answer) } The example is not reproducible for us, since we do not have z. Nor is there a package 'fitCopula' available to us. I'd try to rewrite it as follows, without having tried anything: my.cop - normalCopula(param = 0.5, dim = 2) answer - apply(z[,-1], 2, function(x) fitCopula(cbind(z[,1], x), my.cop, start=0.3), my.cop = my.cop) That is not quite the same thing, as he included i=1 in the loop. Assuming this is package 'copula' the result is an S4 classed object (although that is far from clear on the help page). apply() is not said to work with such functions (and I have little idea what as.vector will do, most likely fail), so I think I would use lapply(). Something like answer - lapply(seq_len(ncol(z)), function(i) fitCopula(cbind(z[,1], z[,i]), my.cop, start=0.3), my.cop = my.cop) [...] -- Brian D. Ripley, [EMAIL PROTECTED] 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@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] for loop problem
Hello R users, A beginners question which I could not find the answer to in earler posts. My thought process: Here z is a 119 x 15 data matrix Step 1: start at column one, bind every column with column 1 Step2: use the new matrix, test, in the fitCopula package Step3: store each result in myfit, bind each result to answer Step4: return answer copula_est - function(z) { for(i in 1:length(z[1,])) { my.cop - normalCopula(param = 0.5, dim = 2) test - cbind(z[,1],z[,i]) myfit[i] - fitCopula(test,my.cop, start=0.3) } answer - cbind(myfit[i]) return(answer) } Errors received: Error: object test not found Could my syntax be incorrect, or is it a deeper faulty logic error. Thank you for your help, it is much appreciated. aat -- View this message in context: http://www.nabble.com/for-loop-problem-tf3047849.html#a8472217 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] for loop problem
Tobias Verbeke [EMAIL PROTECTED] writes: Dear list, Here's a function that works fine when I assign one value to i. qx, ax and gr are vectors. ax.to.nax - function(qx, ax, gr=c(0,1,5,10)){ px - 1 - qx last.n - gr[length(gr)] - gr[length(gr) - 1] all.bounds - c(gr, gr[length(gr)] + last.n) n - diff(all.bounds) # i - 1 # this i should loop through the values of # gr: i=0, i=1, i=5 and i=10. # testprod - prod(px[(i+1):(i+n[i+1])]) return(1 - testprod) } Every attempt to loop through the values of gr, gives the following error message: Error in (i + 1):(i + n[i + 1]) : NA/NaN argument Here is an example of the deplorably erroneous code that causes the message to appear: ax.to.nax.for - function(qx, ax, gr=c(0,1,5,10)){ px - 1 - qx last.n - gr[length(gr)] - gr[length(gr) - 1] all.bounds - c(gr, gr[length(gr)] + last.n) n - diff(all.bounds) testprod - numeric(length(gr)) for (j in 1:length(gr)){ i - gr[j] testprod[j] - prod(px[(i+1):(i+n[i+1])]) } return(1 - testprod) } In what way am I ill-treating R ? You're ill-treating the readers by not giving a full example of a call to the function... However: in the for loop, i will be one of 0,1,5,10 and n is the vector c(1,4,5,5) so n[i+1] is indexing out of bounds and e.g. n[6] [1] NA and the : operator subsequently objects. Did you mean (i+1):(i+n[j]+1) or so? -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] for loop problem
Sir Dalgaard, Thank you for your kind reply. In what way am I ill-treating R ? You're ill-treating the readers by not giving a full example of a call to the function... ax.to.nax.for(qx, ax) However: in the for loop, i will be one of 0,1,5,10 and n is the vector c(1,4,5,5) so n[i+1] is indexing out of bounds and e.g. n[6] [1] NA and the : operator subsequently objects. Did you mean (i+1):(i+n[j]+1) or so? Yes! I meant (i+1):(i+n[j]) Thanks again, Tobias __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] for loop problem
On Tue, 04 Mar 2003, Jeremy Z. Butler told this: I want to generate a sequence which goes 1 2 3 4 5 6 7 8 14 15 16 17 18 19 20 21 26 27 ... i.e. 8 consecutive numbers then 5 missed then the next 8 numbers etc. I was going to do this using the seq() function but couldn't figure out how so I thought I'd try a loop: for (x in seq(1,650,13)) { num.set.1 - x:x+8 ^ should be x:(x+8) } and by all means, avoid for loop, think in vector. Michael __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
RE: [R] for loop problem
Your sequence is 1:a + k*(a+b) where a=8, b=5 and k=0,1,...,K. You can make use of the fact that R loops of vectors if two vectors are not the same; a - 8 b - 5 K - 49 x - rep((0:K)*(a+b), each=a) + 1:a Cheers Henrik Bengtsson -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jeremy Z Butler Sent: den 4 mars 2003 14:14 To: [EMAIL PROTECTED] Subject: [R] for loop problem Hi, I'm just coming to grips with for looping etc. and have a bit of a problem: I want to generate a sequence which goes 1 2 3 4 5 6 7 8 14 15 16 17 18 19 20 21 26 27 ... i.e. 8 consecutive numbers then 5 missed then the next 8 numbers etc. I was going to do this using the seq() function but couldn't figure out how so I thought I'd try a loop: for (x in seq(1,650,13)) { num.set.1 - x:x+8 } but now what I need to do is write code such that each time it goes through the loop it assigns the output to a different object e.g. num.set.1 on the first loop then num.set.2 on the next etc. so that they can be concatenated. Is there any way to do this?? I may be doing this an extremely complicated way but with my zero programming experience its the best I can think of. Can anyone help? J __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/ r-help __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] for loop problem
Jeremy Z Butler [EMAIL PROTECTED] writes: Hi, I'm just coming to grips with for looping etc. and have a bit of a problem: I want to generate a sequence which goes 1 2 3 4 5 6 7 8 14 15 16 17 18 19 20 21 26 27 ... i.e. 8 consecutive numbers then 5 missed then the next 8 numbers etc. I was going to do this using the seq() function but couldn't figure out how so I thought I'd try a loop: for (x in seq(1,650,13)) { num.set.1 - x:x+8 } but now what I need to do is write code such that each time it goes through the loop it assigns the output to a different object e.g. num.set.1 on the first loop then num.set.2 on the next etc. so that they can be concatenated. Is there any way to do this?? I may be doing this an extremely complicated way but with my zero programming experience its the best I can think of. Can anyone help? I suggest using a matrix. mseq = as.vector(matrix(1:650, nrow = 13)[1:8,]) length(mseq) [1] 400 mseq[1:20] [1] 1 2 3 4 5 6 7 8 14 15 16 17 18 19 20 21 27 28 29 30 __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help