How to find the mean of a continuous variable for each categorical variable

后端 未结 2 928
孤独总比滥情好
孤独总比滥情好 2021-01-27 08:21

I\'d like to plot continuous BMI on the y-axis and the categorical variable for Family Income on the x-axis and I\'d like the graph to plot the mean BMI for each category. Howev

相关标签:
2条回答
  • 2021-01-27 08:46

    This may work:

        library(plyr)
        nhh<-ddply(nh,.(famIncome), summarise, mean.bmi=mean(bmi)) # find mean bmi
        with(nhh, plot(famIncome,mean.bmi)) # simple plot
    
    0 讨论(0)
  • 2021-01-27 08:55

    Here's a base solution using aggregate:

    a <- aggregate(BMI ~ FamIncome, data=nh, FUN=mean)
    barplot(a$BMI, names.arg=a$FamIncome)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题