0

I will preface this by saying that I am a complete R novice and have been asked to do some calculations that are way over my head, so please forgive me in advance if this is not the right way to ask this question!!

I have an R data frame that has 2 columns: one is age (18-80) and the other is a dependent variable that has three possible outcomes (0,1,2). I would like to plot a graph that has x = age and y = the average of the dependent variable by age. I know how to make a simple graph and I know how to calculate the average of my (0,1,2) column by age individually, but it seems really labor-intensive to do that manually for every age from 18 to 80 and then plot that against age in a new data frame that I guess I'd have to make.

How do I find the mean of my dependent variable by subset (age) and then plot it against age?

badatR
  • 1
  • 1
  • `plot(aggregate(outcome ~ age, data, mean))` please read this https://stackoverflow.com/a/5963610/2994949 – rawr Mar 05 '21 at 21:18
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Mar 06 '21 at 03:31
  • Thanks so much!! I am barely even to the level of being able to make an example so the links are very helpful. Thankfully I got the answer I was looking for even with my rudimentary question and have a graph now that I am happy with! :) – badatR Mar 06 '21 at 16:02

1 Answers1

0

You could also do this with ggplot:

dat <- data.frame(age=sample(18:80, 250, replace=TRUE), 
                  y = sample(0:2, 250, replace=TRUE))

ggplot(dat, aes(x=age, y=y)) + 
  stat_summary(fun.data = function(y)data.frame(y=mean(y)), 
               geom="line")

enter image description here

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25