[R] Iterations

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue Jul 15 19:37:59 CEST 2008


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.

Entering an expression interactively behaves differently than entering
one in the context of another statement or function.

See the FAQ:

http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html#Why-is-the-output-not-printed-when-I-source_0028_0029-a-file_003f

-- 
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list