R:如何根据其他四个变量的百分比来创建新变量?

时间:2019-08-14 11:55:48

标签: r

我想根据以下要素来计算特定国家/地区的权重:40%的人口,40%的GDP,10%的过往申请数量和10%的失业率。

我的数据如下:

Country   `Population (peop~ `Population (%):` `Total GDP (millio~ `Total GDP (%)` `Number of past appli~ `Number of past app~ `Unemployment rate (% ~
  <chr>                  <dbl>             <dbl>               <dbl>           <dbl>                  <dbl>                <dbl>                   <dbl>
1 Austria              8822267           0.0168                 113.          0.0324                 196875             0.0415                       4.9
2 Belgium             11398589           0.0216                 108.          0.0311                 126520             0.0266                       6  
3 Bulgaria             7050034           0.0134                 103.          0.0296                  57120             0.0120                       5.2
4 Croatia              4105493           0.00780                100.          0.0289                   4660             0.000981                     8.4
5 Cyprus                864236           0.00164                103.          0.0297                  19315             0.00407                      8.4
6 Czech Re~           10610055           0.0202                 115           0.0330                   7270             0.00153                      2.2

1 个答案:

答案 0 :(得分:1)

假设您要为变量分配不同的权重并计算new_var,则可以执行以下操作:

library(dplyr)
my_df <- read.csv("df.csv", header = T)
my_df %>% 
  mutate(new_var = ((.4*pop_per)+(.4*gdp_perc) + (.1*no_past_perc) + (.1*unRate)))
# -------------------------------------------------------------------------
#  Country population pop_per TotalGDP gdp_perc NoOfPastApplications no_past_perc unRate   new_var
# 1  Austria    8822267 0.01680      113   0.0324               196875     0.041500    4.9 0.5138300
# 2  Belgium   11398589 0.02160      108   0.0311               126520     0.026600    6.0 0.6237400
# 3 Bulgaria    7050034 0.01340      103   0.0296                57120     0.012000    5.2 0.5384000
# 4 Croatia     4105493 0.00780      100   0.0289                 4660     0.000981    8.4 0.8547781
# 5 Cyprus       864236 0.00164      103   0.0297                19315     0.004070    8.4 0.8529430
# 6 Czech Re   10610055 0.02020      115   0.0330                 7270     0.001530    2.2 0.2414330