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