# Transformácia a súhrn
# Virych Hlieb
# 2024-03-06
# 1.načítať tabuľku údajov UN z balíku carData,

library(carData)
## Warning: пакет 'carData' был собран под R версии 4.3.3
library(dplyr)
## 
## Присоединяю пакет: 'dplyr'
## Следующие объекты скрыты от 'package:stats':
## 
##     filter, lag
## Следующие объекты скрыты от 'package:base':
## 
##     intersect, setdiff, setequal, union
data(UN)

# 2.premenovať stĺpce z angličtiny do svojho rodného jazyka,

UN <- UN %>%
  rename(
    Країна = region,
    Група = group,
    'народжуваність дітей' = fertility,
    Продуктивність_робітника = ppgdp,
    Урбанізація = lifeExpF,
    Місто = pctUrban,
    'дитяча смертність' = infantMortality
  )

# 3.ponechať v tabuľke iba európske krajiny,

UN <- UN %>%
  filter(Країна == "Europe")

# 4.prekódovať hodnoty premennej skupina na {OECD, ostatné},

UN <- UN %>%
  mutate(Група = if_else(Група %in% c("OECD", "EU"), "OECD", "other"))

# 5.vytvoriť novú premennú konverziou percenta mestskej populácie na podiel v intervale [0,1] (stĺpec umiestniť hneď vedľa),

UN <- UN %>%
  mutate(Пропорція_урбанізації = Урбанізація / 100)

# 6.zoskupiť pozorovania podľa skupiny,

grouped_UN <- UN %>%
  group_by(Група) %>%
  summarise(across(where(is.numeric), mean))

print(grouped_UN)
## # A tibble: 1 × 7
##   Група `народжуваність дітей` Продуктивність_робітника Урбанізація Місто
##   <chr>                  <dbl>                    <dbl>       <dbl> <dbl>
## 1 other                   1.59                   27394.        80.7  70.5
## # ℹ 2 more variables: `дитяча смертність` <dbl>, Пропорція_урбанізації <dbl>
# 7.vypočítať priemerné hodnoty všetkých numerických premenných okrem percenta urbanizácie (napríklad pomocou across a where),

mean_values <- UN %>%
  summarise(across(where(is.numeric) & !contains(names(.), "Урбанізація", ignore.case = FALSE), mean))


print(mean_values)
##   народжуваність дітей Продуктивність_робітника Урбанізація    Місто
## 1             1.590026                 27393.94    80.69077 70.46154
##   дитяча смертність Пропорція_урбанізації
## 1          6.121744             0.8069077
# 8.zoradiť podľa priemerného veku dožitia vzostupne.

sorted_UN <- UN %>%
  arrange(Урбанізація)

print(sorted_UN)
##                        Країна Група народжуваність дітей
## Moldova                Europe other                1.450
## Ukraine                Europe other                1.483
## Russian Federation     Europe other                1.529
## Belarus                Europe other                1.479
## Serbia                 Europe other                1.562
## Bulgaria               Europe other                1.546
## TFYR Macedonia         Europe other                1.397
## Montenegro             Europe other                1.630
## Romania                Europe other                1.428
## Lithuania              Europe other                1.495
## Bosnia and Herzegovina Europe other                1.134
## Hungary                Europe other                1.430
## Latvia                 Europe other                1.506
## Slovakia               Europe other                1.372
## Estonia                Europe other                1.702
## Croatia                Europe other                1.501
## Albania                Europe other                1.525
## Poland                 Europe other                1.415
## Czech Republic         Europe other                1.501
## Denmark                Europe other                1.885
## Malta                  Europe other                1.284
## United Kingdom         Europe other                1.867
## Greece                 Europe other                1.540
## Luxembourg             Europe other                1.683
## Portugal               Europe other                1.312
## Netherlands            Europe other                1.794
## Belgium                Europe other                1.835
## Slovenia               Europe other                1.477
## Germany                Europe other                1.457
## Ireland                Europe other                2.097
## Finland                Europe other                1.875
## Norway                 Europe other                1.948
## Austria                Europe other                1.346
## Sweden                 Europe other                1.925
## Iceland                Europe other                2.098
## Italy                  Europe other                1.476
## Switzerland            Europe other                1.536
## Spain                  Europe other                1.504
## France                 Europe other                1.987
##                        Продуктивність_робітника Урбанізація Місто
## Moldova                                  1625.8       73.48    48
## Ukraine                                  3035.0       74.58    69
## Russian Federation                      10351.4       75.01    73
## Belarus                                  5702.0       76.37    75
## Serbia                                   5123.2       77.05    56
## Bulgaria                                 6365.1       77.12    72
## TFYR Macedonia                           4434.5       77.14    59
## Montenegro                               6509.8       77.37    61
## Romania                                  7522.4       77.95    58
## Lithuania                               10975.5       78.28    67
## Bosnia and Herzegovina                   4477.7       78.40    49
## Hungary                                 12884.0       78.47    68
## Latvia                                  10663.0       78.51    68
## Slovakia                                15976.0       79.53    55
## Estonia                                 14135.4       79.95    70
## Croatia                                 13819.5       80.37    58
## Albania                                  3677.2       80.40    53
## Poland                                  12263.2       80.56    61
## Czech Republic                          18838.8       81.00    74
## Denmark                                 55830.2       81.37    87
## Malta                                   19599.2       82.29    95
## United Kingdom                          36326.8       82.42    80
## Greece                                  26503.8       82.58    62
## Luxembourg                             105095.4       82.67    85
## Portugal                                21437.6       82.76    61
## Netherlands                             46909.7       82.79    83
## Belgium                                 43814.8       82.81    97
## Slovenia                                23109.8       82.84    49
## Germany                                 39857.1       82.99    74
## Ireland                                 46220.3       83.17    62
## Finland                                 44501.7       83.28    85
## Norway                                  84588.7       83.47    80
## Austria                                 45158.8       83.55    68
## Sweden                                  48906.2       83.65    85
## Iceland                                 39278.0       83.77    94
## Italy                                   33877.1       84.62    69
## Switzerland                             68880.2       84.71    74
## Spain                                   30542.8       84.76    78
## France                                  39545.9       84.90    86
##                        дитяча смертність Пропорція_урбанізації
## Moldova                           14.344                0.7348
## Ukraine                           11.822                0.7458
## Russian Federation                10.534                0.7501
## Belarus                            6.494                0.7637
## Serbia                            10.630                0.7705
## Bulgaria                           9.149                0.7712
## TFYR Macedonia                    13.063                0.7714
## Montenegro                         7.733                0.7737
## Romania                           12.216                0.7795
## Lithuania                          5.941                0.7828
## Bosnia and Herzegovina            12.695                0.7840
## Hungary                            5.304                0.7847
## Latvia                             6.700                0.7851
## Slovakia                           5.676                0.7953
## Estonia                            4.382                0.7995
## Croatia                            5.571                0.8037
## Albania                           16.561                0.8040
## Poland                             5.546                0.8056
## Czech Republic                     2.997                0.8100
## Denmark                            3.914                0.8137
## Malta                              5.405                0.8229
## United Kingdom                     4.702                0.8242
## Greece                             4.488                0.8258
## Luxembourg                         2.289                0.8267
## Portugal                           4.175                0.8276
## Netherlands                        4.168                0.8279
## Belgium                            3.739                0.8281
## Slovenia                           3.279                0.8284
## Germany                            3.487                0.8299
## Ireland                            3.859                0.8317
## Finland                            2.783                0.8328
## Norway                             2.940                0.8347
## Austria                            3.713                0.8355
## Sweden                             2.544                0.8365
## Iceland                            2.057                0.8377
## Italy                              3.417                0.8462
## Switzerland                        3.513                0.8471
## Spain                              3.573                0.8476
## France                             3.345                0.8490