[R] Iterations

Stephan Kolassa Stephan.Kolassa at gmx.de
Tue Jul 15 19:37:11 CEST 2008


Kevin,

By default, many functions only *return* a result, they don't explicitly 
*print* it. There is no difference in interactive mode, but there is in 
batch mode (e.g., in loops). Use print() or cat() for explicit printing 
to console.

for(i in 1:100)
{
     cat(i,"\n")
}

HTH,
Stephan


rkevinburton at charter.net schrieb:
> 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