Re: [R] for loop question in R

2021-12-22 Thread Kai Yang via R-help
Hi Rui and Ivan,Thank you explain of the code for me in detail. This is very helpful. And the code works well now.Happy Holiday,Kai On Wednesday, December 22, 2021, 02:30:49 PM PST, Rui Barradas wrote: Hello, y[i] and c[i] are character strings, they are not variables of data set mpg.

Re: [R] for loop question in R

2021-12-22 Thread Rui Barradas
Hello, There's a stupid typo in my previous post. Inline Às 22:30 de 22/12/21, Rui Barradas escreveu: Hello, y[i] and c[i] are character strings, they are not variables of data set mpg. To get the variables, use, well, help("get"). Note that I have changed the temp dir to mine. So I created

Re: [R] for loop question in R

2021-12-22 Thread Rui Barradas
Hello, y[i] and c[i] are character strings, they are not variables of data set mpg. To get the variables, use, well, help("get"). Note that I have changed the temp dir to mine. So I created a variable to hold the value tmpdir <- "c:/temp/" for (i in seq(nrow(mac))){ mpg %>% filter(hwy

Re: [R] for loop question in R

2021-12-22 Thread Kai Yang via R-help
strange, I got error message when I run again: Error: unexpected symbol in: "    geom_point()   ggsave" > } Error: unexpected '}' in "}" On Wednesday, December 22, 2021, 10:18:56 AM PST, Kai Yang wrote: Hello Eric, Jim and Ivan, Many thanks all of your help. I'm a new one in R area. I

Re: [R] for loop question in R

2021-12-22 Thread Kai Yang via R-help
Hello Eric, Jim and Ivan, Many thanks all of your help. I'm a new one in R area. I may not fully understand the idea from you.  I modified my code below, I can get the plots out with correct file name, but plots  are not using correct fields' name. it use y[i], and c[i] as variables' name, does

Re: [R] for loop question in R

2021-12-22 Thread Ivan Krylov
On Wed, 22 Dec 2021 16:58:18 + (UTC) Kai Yang via R-help wrote: > mpg %>%    filter(hwy <35) %>%     ggplot(aes(x = displ, y = y[i], > color = c[i])) +     geom_point() Your code relies on R's auto-printing, where each line of code executed at the top level (not in loops or functions) is run

Re: [R] for loop question in R

2021-12-22 Thread jim holtman
You may have to add an explicit 'print' to ggplot library(ggplot2) library(tidyverse) y <- c("hwy","cty") c <- c("cyl","class") f <- c("hwy_cyl","cty_class") mac <- data.frame(y,c,f) for (i in nrow(mac)){ mpg %>%filter(hwy <35) %>% print(ggplot(aes(x = displ, y = y[i], color = c[i])) +

Re: [R] for loop question in R

2021-12-22 Thread Eric Berger
Try replacing "c:/temp/f[i].jpg" with paste0("c:/temp/",f[i],".jpg") On Wed, Dec 22, 2021 at 7:08 PM Kai Yang via R-help wrote: > Hello R team,I want to use for loop to generate multiple plots with 3 > parameter, (y is for y axis, c is for color and f is for file name in > output). I created a

Re: [R] for loop question in R

2021-12-22 Thread Andrew Simmons
nrow() is just the numbers of rows in your data frame, use seq_len(nrow()) or seq(nrow()) to loop through all row numbers On Wed, Dec 22, 2021, 12:08 Kai Yang via R-help wrote: > Hello R team,I want to use for loop to generate multiple plots with 3 > parameter, (y is for y axis, c is for color a

[R] for loop question in R

2021-12-22 Thread Kai Yang via R-help
Hello R team,I want to use for loop to generate multiple plots with 3 parameter, (y is for y axis, c is for color and f is for file name in output). I created a data frame to save the information and use the information in for loop. I use y[i], c[i] and f[i] in the loop, but it seems doesn't wor

Re: [R] For loop question

2012-11-10 Thread David Winsemius
On Nov 10, 2012, at 10:17 PM, dae wrote: > Thanks. That got me the answer. Good. That's what the FAQ is for. You should also read the Posting Guide where the reasons behind the request to include context for replies is laid out. > This works: > > symbols = c("IEF","SPY") > > getSymbols(sym

Re: [R] For loop question

2012-11-10 Thread dae
Thanks. That got me the answer. This works: symbols = c("IEF","SPY") getSymbols(symbols) for(symbol in symbols) { assign(symbol, to.monthly(get(symbol), indexAt="endof")) } #end -- View this message in context: http://r.789695.n4.nabble.com/For-loop-question-tp4649215p46492

Re: [R] For loop question

2012-11-10 Thread David Winsemius
On Nov 10, 2012, at 2:36 PM, dae wrote: > I have this code: > IEF <- to.monthly(IEF, indexAt="endof") > SPY <- to.monthly(SPY, indexAt="endof") > > I would like to use a for loop instead of separate entries, > so the only code that needs to be modified is the list > of symbols. > > symbols <- c

[R] For loop question

2012-11-10 Thread dae
I have this code: IEF <- to.monthly(IEF, indexAt="endof") SPY <- to.monthly(SPY, indexAt="endof") I would like to use a for loop instead of separate entries, so the only code that needs to be modified is the list of symbols. symbols <- c("IEF", "SPY") for(symbol in symbols) { symbol <- to.m

Re: [R] for loop question Documentation and its application for calculating euclidean distance on MDS ordination axis scores

2008-10-01 Thread Ben Bolker
> I will post data probably tonight, but here is my problem. I have > preformed an MDS on a set of data. I have the scores of the four axes > that > are the optimal solution. I want to calculate the euclidean distance > between time steps of the ordination. See ?dist for a much faster sol

Re: [R] for loop question Documentation and its application for calculating euclidean distance on MDS ordination axis scores

2008-10-01 Thread Gábor Csárdi
Actually, help.search("for") finds Control(base) Control Flow which is exactly where 'for' is documented. In general, if you want the manual page of reserved words, then you'll have to quote them: ?"for" Gabor On Wed, Oct 1, 2008 at 3:39 PM, stephen sefick <[EMAIL PROTECTED]> wrote:

Re: [R] for loop question Documentation and its application for calculating euclidean distance on MDS ordination axis scores

2008-10-01 Thread Gabor Grothendieck
One must write ?"for" presumably since for is a reserved word in R. On Wed, Oct 1, 2008 at 9:39 AM, stephen sefick <[EMAIL PROTECTED]> wrote: > ?for doesn't return anything help.search("for") doesn't return anything- > Is the for loop so prevelant in computer programing that the > documentation is

[R] for loop question Documentation and its application for calculating euclidean distance on MDS ordination axis scores

2008-10-01 Thread stephen sefick
?for doesn't return anything help.search("for") doesn't return anything- Is the for loop so prevelant in computer programing that the documentation is implicit or is R paradigm to discourage the use of the for loop. I will post data probably tonight, but here is my problem. I have preformed an MD

Re: [R] R for loop question

2008-05-20 Thread jim holtman
Consider using a 'list' instead of creating a lot of objects that you then have to manage: x <- lapply(1:length(stats$hour), function(.indx) dataset[.indx, 3:15]) You can then access the data as x[[1]], ... On Tue, May 20, 2008 at 12:58 PM, Douglas M. Hultstrand < [EMAIL PROTECTED]> wrote: > He

Re: [R] R for loop question

2008-05-20 Thread Erik Iverson
Take a look at ?assign Juan Manuel Barreneche wrote: I had to do the same thing many times, i usually use a combination of the functions "eval", "parse" and "sprinf", as below: k <- 1 for (i in 1:length(stats$hour)) { eval(parse(text=sprintf("x%s <- dataset[%s,(3:15)]", i, k))) k <- k+1

Re: [R] R for loop question

2008-05-20 Thread Juan Manuel Barreneche
I had to do the same thing many times, i usually use a combination of the functions "eval", "parse" and "sprinf", as below: k <- 1 for (i in 1:length(stats$hour)) { eval(parse(text=sprintf("x%s <- dataset[%s,(3:15)]", i, k))) k <- k+1 } what it does is: eval(parse(text=STRING)) is a way to

Re: [R] R for loop question

2008-05-20 Thread Erik Iverson
Douglas - To answer your question directly, use perhaps combination of ?assign and ?paste. In general, you usually do not have to do this sort of thing, but can use one of the apply family of functions (apply, sapply, lapply, mapply) to do whatever you want with shorter, cleaner code and few

[R] R for loop question

2008-05-20 Thread Douglas M. Hultstrand
Hello, I am trying to assign a variable name (x1,x2,x3...) in a loop statement that is based on a counter (counter is based on the number of hours within the datafile). The x1,x2 data will later be called for plotting the data. Below is a clip of the for loop I am using, any suggestions? k