0

I'm trying to get the average weight and height by age (along with other variables) from my dataset using the aggregate function but can't get it to work.

The following code gives me everything from the min, quartiles, mean etc. but I only want the mean.

table1 <- aggregate(data[,c("height", "weight")], by=list(age=data$age), summary)
View(table1)

I've tried this instead but I just get a table of height and weight by all my ages filled with NA values.

table1 <- aggregate(data[,c("height", "weight")], by=list(age=data$age), mean)
View(table1)

Thank you for any help.

David
  • 5
  • 2
  • 2
    Your first `table1` should have columns named `height.Mean` and `weight.Mean`. If not, then there is something wrong that we cannot know without data. If they do and you don't want the rest of the fields, then replace `summary` with `mean` and you'll have three columns: `"age"`, `"height"`, and `"weight"`. Please [edit] your question and paste the output from `dput(head(data))` into a code-block. Thanks! (A good ref for summarizing by group, btw: https://stackoverflow.com/q/11562656/3358272) – r2evans Dec 24 '22 at 15:24

1 Answers1

0

Try passing na.rm=TRUE to the mean function:


aggregate(data[,c("height", "weight")], by=list(age=data$age),\(x) mean(x,na.rm=T))


 
user12256545
  • 2,755
  • 4
  • 14
  • 28
  • This worked thank you! I was wondering if you could provide a little more explanation as to why this works? Specifically, what does \ (x) do? – David Dec 24 '22 at 17:06