[R] Iterations

Erik Iverson iverson at biostat.wisc.edu
Tue Jul 15 22:09:46 CEST 2008


Sorry, I'm in ESS.

Try ?Control

rkevinburton at charter.net wrote:
> Thank you.
> 
> ?for just gives me a + rompt indicating that I need to supply more input. The same with ?while and ?repeat. Help(for) yelds:
> 
>  > help(for)
> Error: unexpected ')' in "help(for)"
> 
> But thanks for the tip.
> 
> Keivn
> 
> ---- Erik Iverson <iverson at biostat.wisc.edu> wrote: 
>> If you read the help page, ?for, you might have seen under "Value", that
>>
>>       'for', 'while' and 'repeat' return the value of the last
>>       expression evaluated (or 'NULL' if none was), invisibly.
>>
>> So if you want to see the values, print() them.
>>
>> In general, from the first part of your message, it looks like you're 
>> trying to run some analysis on different subgroups of your data.
>>
>> You may want to try the functions tapply, by, aggregate, ave, etc., for 
>> this purpose rather than using 'for' loops.
>>
>> Best,
>> Erik Iverson
>>
>>
>> rkevinburton at charter.net wrote:
>>> I have a command that reads in some data:
>>>
>>> x <- read.csv("Sales2007.dat", header=TRUE)
>>>
>>> Then I try to organize the data:
>>>
>>> sc <- split(x, list(x$Category, x$SubCategory), drop=TRUE)
>>>
>>> Then I want to iterate through the data. I was able to get the following to run on the R console:
>>>
>>> for(i in 1:length(sc))
>>> {
>>>     sum(sc[[i]]$Quantity)
>>> }
>>>
>>> But notiing is primted on the console. I find that:
>>>
>>> for(i in 1:100)
>>> {
>>>     i
>>> }
>>>
>>> Also does't output anything? I am probably making a wrong assumption here. Why desn't the loop seem to output anyything?
>>>
>>> Thank you.
>>>
>>> Kevin
>>>
>>> ______________________________________________
>>> R-help at 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.
>



More information about the R-help mailing list