[R] question about for loop

Kai Yang y@ngk@|9999 @end|ng |rom y@hoo@com
Fri Dec 24 22:48:00 CET 2021


Hello Team,
I create a csv file (mac2) to save parameter values. the file looks like:

y,c,f
hwy,cyl,hwy_cyl2
cty,class,cty_class2

Then I load the file into R and apply the parameters y, c, f in for loop, see my code below:
library(ggplot2)
library(tidyverse)
library(readr)
mac2 <- read_csv("C:/temp/mac2.csv")
View(mac2)
for (i in seq(nrow(mac2))){
  mpg %>%
    filter(hwy <35) %>% 
    ggplot(aes(x = displ, y = get(y[i]), color = get(c[i]) )) + 
    geom_point()+
    ylab(y[i]) +                              
    guides(color = guide_legend(title = c[i]))    ggsave(paste0("c:/temp/",f[i],".jpg"),width = 9, height = 6, dpi = 1200, units = "in")
}

but I got an error message: "Error in dots_list(..., title = title, subtitle = subtitle, caption = caption,  :  object 'y' not found"
Does anyone know how to fix the problem?
Thanks,
Kai


	[[alternative HTML version deleted]]



More information about the R-help mailing list