简体   繁体   中英

Finding Average of One Column Based on 2 Other Columns RStudio

I currently have a data frame that has three columns (City, State and Income) I wrote an example of the data below...

City     State     Income 
Addison  Illinois   71,000
Addison  Illinois   101,000
Addison  Illinois   81,000
Addison  Texas      74,000

As you can see there are repeats of the cities. There are several Addison, IL's because income differs by the zip-code/area of the city.

I want to take the average of all incomes in a given city and state. In this example I want the average of all Addison IL's but NOT including Addison, Texas.

I am looking for this (in this given example)

City    State    MeanIncome
Addison Illinois  84,333
Addison Texas     74,000

I tried this:

Income_By_City <- aggregate( Income ~ City, df, mean ) 

But it gave me the average of ALL Addison's, including Texas...

Is there a way to take the average of Income Column, based on City AND State??

I am pretty new to coding, so I'm not sure if this is a simple question. But I would appreciate any help I can get.

df <- data.frame(City = c("Addison", "Addison", "Addison", "Addison"), State = c("Illinois", "Illinois", "Illinois", "Texas"), Income = c(71000, 101000, 81000, 74000))

library(dplyr)
df %>%
   group_by(City, State) %>%
   summarise(MeanIncome=(mean(Income)))


#     City     State   MeanIncome
#1 Addison  Illinois   84333.33
#2 Addison     Texas   74000.00

Here is a dplyr solution:

library(tidyverse)

df <- tribble(
~City,      ~State,      ~Income, 
"Addison",  "Illinois",  71000,
"Addison",  "Illinois",  101000,
"Addison",  "Illinois",  81000,
"Addison",  "Texas",     74000
)

df %>% 
  group_by(City, State) %>% 
  mutate(AverageIncome = mean(Income))

# A tibble: 4 x 4
# Groups:   City, State [2]
     City    State Income AverageIncome
    <chr>    <chr>  <dbl>         <dbl>
1 Addison Illinois  71000      84333.33
2 Addison Illinois 101000      84333.33
3 Addison Illinois  81000      84333.33
4 Addison    Texas  74000      74000.00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM