2017-12-07 4 views
0

私は2つのデータフレームを持っています:都市と国。私は国ごとに最も人気のある都市を見つけようとしています。都市と国に共通のフィールド、City.CountryCodeとCountry.Codeがあります。これらの2つのデータフレームは、CityCountryと呼ばれるものにマージされました。r集合関数 - 追加の列を表示する方法

aggregate(Population.x~CountryCode, CityCountry, max) 

この集約コマンドでは、CountryCode列とPopulation.X列のみが表示されます。国名と都市名をどのように表示しますか?ここで使用する間違ったコマンドを集約していますか?

+1

、[どのようにの ' – akrun

+2

可能な重複[(CityCountry、Population.x == AVE(Population.x、国番号、FUN = MAX))、付]' ave'すなわち 'CityCountryを使用dplyrで各グループの最大値を持つ行を選択しますか?](https://stackoverflow.com/questions/24237399/how-to-select-the-rows-with-maximum-values-in-each-group-with- dplyr)より一般的には[各グループの最大値を持つ行を選択する方法](https://stackoverflow.com/questions/24558328/how-to-select-the-row-with-the-maximum-value-それぞれのグループで)。また、[データフレーム内の各グループ内の最大値を抽出する](https://stackoverflow.com/questions/25314336/extract-the-maximum-value-within-each-group-in-a-dataframe) –

答えて

1

Countryでグループ化し、次にmax(Population.x)でフィルタすると、dplyrを使用することもできます。その場合

library(dplyr) 

set.seed(123) 
CityCountry <- data.frame(Population.x = sample(1000:2000, 10, replace = TRUE), 
          CountryCode = rep(LETTERS[1:5], 2), 
          Country = rep(letters[1:5], 2), 
          City = letters[11:20], 
          stringsAsFactors = FALSE) 

CityCountry %>% 
    group_by(Country) %>% 
    filter(Population.x == max(Population.x)) %>% 
    ungroup() 

# A tibble: 5 x 4 
    Population.x CountryCode Country City 
     <int>  <chr> <chr> <chr> 
1   1287   A  a  k 
2   1789   B  b  l 
3   1883   D  d  n 
4   1941   E  e  o 
5   1893   C  c  r 
関連する問題