# [R] for loop

Daniel Malter daniel at umd.edu
Wed Aug 5 21:11:16 CEST 2009

```No, you did not. To show what David, Jim, or I are talking about, consider
this example:

x1=rnorm(100,5,1)
x2=rnorm(100,3,2)
e=rnorm(100)
y1=x1+e
y2=2*x2+e

x=data.frame(x1,x2)
y=data.frame(y1,y2)
results=list
for(i%in%length(x)){
print(summary(lm(y[,i]~x[,i])))
}

##WHOOPS, THIS ONLY RUNS REGRESSION ON THE LAST COLUMNS OF Y AND X,
respectively

for(i in 1:length(x)){
print(summary(lm(y[,i]~x[,i])))
}

##UJUIJUIJUI, THIS RUNS TWO REGRESSIONS ON THE FIRST AND SECOND COLUMNS OF Y
AND X, respectively

##LOOKS SIMILAR TO YOUR PROBLEM DOESN'T IT?

This is code that, when copied into the R-prompt, runs through. If there is
a mistake in the programming, then it very likely gives out the error and
gives your helpers the idea where your problem lies. And again, potentially
you would find the problem yourself in the process of creating this
self-contained example. However, without a self-contained example, it is
impossible for us to know whether the error lies here or in one of the many
other reasons (e.g. on of the functions called in your MODELS function).

Daniel

-------------------------
cuncta stricte discussurus
-------------------------

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von waltzmiester
Gesendet: Wednesday, August 05, 2009 2:37 PM
An: r-help at r-project.org
Betreff: Re: [R] for loop

Um I still followed the guidelines...

David Winsemius wrote:
>
>
> On Aug 5, 2009, at 1:22 PM, waltzmiester wrote:
>
>>
>> Jim
>>
>> Settle down, just because you can't understand my post doesn't mean I
>> 1)The code is commented.
>> 2)The problem in the code is succinct and therefore "minimal" even
>> though it cannot be self contained, the user-defined function itself
>> is.
>
> You could for instance have simplified the problem to simply defining
> a simple function outside the loop and then executing it within the
> loop (and probably getting only the result of the last evaluation ....
> as would be expected in R.)
>
>> 3) In order for you to be able to reproduce my code, you would need
>> to to install 13 packages and blend in 187 lines of patch code that I
>> would need to send you.
>
> carefully and thoroughly. What happens, for instance, if you wrap
> print() around the Models call? Or perhaps assign whatever it returns
> to an enduring object as an element of a list?
>
>>
>> I meant that it will model (with function Models) the first species
>> in i, and will not model any of the others.
>>
>> -C
>>
>>
>>
>> jholtman wrote:
>>>
>>> What do you mean by "stop"?  Is there an error message?  What are
>>> you getting as output?  I don't see you saving or printing the
>>> output from
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> On Wed, Aug 5, 2009 at 11:37 AM, waltzmiester<cwalte03 at shepherd.edu>
>>> wrote:
>>>>
>>>> I am trying to get the function "Models" to work each time there is
>>>> an instance of k. This code will stop after the first model is
>>>> complete. I need it to come back and pass the next value of c into
>>>> the "Initial.State"
>>>> function. any ideas?
>>>>
>>>>
>>>> col<-c(23:28)
>>>>
>>>> #Setup
>>>> for(k in col){
>>>> Initial.State(Response=zample[,c(k,29)],
>>>> Explanatory=zample[,variable_columns],
>>>> IndependentResponse=population[,c(k,29)],
>>>> IndependentExplanatory=population[,variable_columns])
>>>>
>>>> #Modeling
>>>> Models(GLM=T, GAM=T, RF=T, GBM=T, TSS=T, KeepPredIndependent=T) }
>>>>
>>>>
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

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

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help