# New to R would like to find a way to find the mean of each states

Hello,
So I was wondering if there was a simpler way to find the mean of all the USA states mean of each population.

So we have a bunch of counties here is a example of how the data.frame is organized

``````   county.name state.name state.abbr pop.size pop.density poverty age.19_under
1      Autauga    Alabama         AL    48612          82    10.4         26.9
2      Baldwin    Alabama         AL   162586         102    10.2         23.5
3      Barbour    Alabama         AL    28414          32    22.1         24.3
``````

I would like to know if there is a way to get the mean of each states pop.size without having to write out each mean by creating a bunch of new data.frames.

Is is grouping the numbers together in the post but they are seperate. It is not all one number

To find the mean population of all of the counties in each state, use the

data %>%
group_by(group) %>%
summarize(mean_ctypop = mean(county_pop)

construction,

as in the toy example below with only 2 states and 5 counties

``````library(tidyverse) #load library

# now build vectors for the toy example
cty <- c("Autauga", "Baldwin", "Baldwin", "Washtenaw", "Wayne")
state <- c(rep("Alabama", 3), rep("Michigan", 2))
pop <- c(48612, 162586, 28414, 1753893, 370963)

# now assemble into a data frame from vectors
df <- data.frame(cty, state, pop)

df #  show the dataframe df

# now run the pipe
df %>%          # take the data frame, then
group_by(state) %>%    # group_by state, then
summarize(mean_cty = mean(pop))  # summarize with a new variable, mean_cty

#> # A tibble: 2 x 2
#>   state    mean_cty
#>   <fct>       <dbl>
#> 1 Alabama    79871.
#> 2 Michigan 1062428
``````

You will find that this construction of

data %>%
group_by(group) %>%
summarize(make a new variable)

comes in handy often

Created on 2019-09-05 by the reprex package (v0.3.0)

1 Like

Take a look at this FAQ to learn how to format plain text and code in your posts here

1 Like

Thank you very much for the help I will try that out. You have saved me a lot of typing

Whop my fault I will look that over thank you for your help in making it look nice

1 Like

That formatted code does look a lot better. To format the code in my response, I used the reprex package, which is very helpful for making reproducible examples for this kind of posting. Reprex takes code that you have copied and formats it nicely to your clipboard for later pasting to a message board like this one.

Tutorials on how to use reprex can be found here

Welcome to the RStudio Community!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.