1

I am new to R and was wondering if there was a way I can simplify my code using an apply function.

In my datafram there are 15 columns - y, a, b, c, d, e, f, g, h, i, j .... o

Instead of:

plot (y~a)
plot (y~b)
.
.
.
plot (y~o)

Is it possible to simplify using an apply function or some other method to enact "plot" over mutiple columns?

  • 1
    [Does this same same post answer your question](https://stackoverflow.com/questions/27952653/how-to-loop-repeat-a-linear-regression-in-r) – Daniel_j_iii Aug 02 '20 at 11:05

1 Answers1

1

There is probably a better solution with but since this is not a reproducible example, here is an approach with matplot():

matplot(x = mtcars[, "carb"], 
        y = mtcars[c("drat", "wt")],
        type = "b",
        pch = 1,
        xlab = "carb",
        ylab = "drat and wt")

The issue with apply() or other base functions is that you would need to determine ahead of time what the xlim and ylim are ahead of time. Otherwise your plots will vary and not look great. That's why ggplot2 has some great features but I would need some sample data, first.

Cole
  • 11,130
  • 1
  • 9
  • 24