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
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. >
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
for(1 in 1:10) { print(i) } Mike On Tue, Jul 15, 2008 at 1:11 PM, <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. >-- -- Michael D. Rennie Ph.D. Candidate University of Toronto at Mississauga 3359 Missisagua Rd. N. Mississauga, ON L5L 1C6 Ph: 905-828-5452 Fax: 905-828-3792 www.utm.utoronto.ca/~w3rennie
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.