0

Nice to meet you all. This is my first question in this wonderful place. Thank you in advance for your time and problem-solving efforts.

I want to see the top 10 donor countries' names each year.

My data looks like this.

 head(Afghanistan)
# A tibble: 6 × 62
  Donor    Y_1960 Y_1961 Y_1962 Y_1963 Y_1964 Y_1965 Y_1966 Y_1967 Y_1968 Y_1969 Y_1970 Y_1971 Y_1972 Y_1973 Y_1974 Y_1975 Y_1976 Y_1977 Y_1978
  <chr>     <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 Austral…     NA     NA     NA     NA  NA      0.03   0.04   0.04   0.06   0.74   0.1    0.12   0.15   0.58   0.15   0.25   0.26   0.65   1.03
2 Austria      NA     NA     NA     NA   0.02   0.02   0.03   0.05   0.02   0.01  NA      0.05  NA     NA     NA     NA      0.03   0.09   0.08
3 Belgium      NA     NA     NA     NA  NA     NA     NA     NA     NA     NA     NA     NA     NA      0.01   0.01   0.01   0.04   0.03   0.06
4 Canada       NA     NA     NA     NA  NA      0.01   0.03   0.03   0.01   0.01   0.7    2.14   1.14   2.22   0.54   1.47   0.38   0.34   4.19
5 Czech R…     NA     NA     NA     NA  NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA   
6 Denmark      NA     NA     NA     NA  NA     NA     NA      0.01   0.02   0.02   0.02   0.02   0.05   0.12   0.05   0.93   2.57   0.06   0.41
# … with 42 more variables

head(DAC)
  iso2c country year unemp unemp_ILO gdpgrowth    gdppc iso3c
6     AT Austria 1965    NA        NA  3.480175 2.810013   AUT
7     AT Austria 1966    NA        NA  5.642861 4.904479   AUT
8     AT Austria 1967    NA        NA  3.008048 2.241010   AUT
9     AT Austria 1968    NA        NA  4.472313 3.931242   AUT
10    AT Austria 1969   2.8        NA  6.275867 5.909496   AUT
11    AT Austria 1970   2.4        NA  6.321143 5.950497   AUT

Before making a for loop, I tried this to test the code.

new <- Afghanistan %>% 
  select(Donor, Y_1990) %>% 
  arrange(desc(Y_1990)) %>% 
  drop_na() %>% 
  slice_max(Y_1990, n=10)
new

# A tibble: 10 × 2
   Donor          Y_1990
   <chr>           <dbl>
 1 United States   56   
 2 Sweden          16.0 
 3 Germany          8.07
 4 Norway           3.35
 5 Netherlands      3.13
 6 Canada           2.73
 7 United Kingdom   2.39
 8 Switzerland      2.01
 9 France           1.87
10 Denmark          1.58

When I run the above code, arrange() works well. However, after using it within for loop, it only shows alphabetical order by country names.

year <- seq(1960, 2020, 1)

for(i in year){
  Year <- paste0("Y", "_", i)
  new <- Afghanistan %>% 
    select(Donor, Year) %>% 
    arrange(desc(Year)) %>% 
    drop_na() 
  print(new[1:10, 1])
  }

# A tibble: 10 × 2
   Donor     Y_1990
   <chr>      <dbl>
 1 Australia   1.23
 2 Austria     1.49
 3 Belgium     0.01
 4 Canada      2.73
 5 Denmark     1.58
 6 Finland     0.15
 7 France      1.87
 8 Germany     8.07
 9 Ireland     0.05
10 Japan       0.03
# A tibble: 10 × 2
   Donor     Y_1991
   <chr>      <dbl>
 1 Australia   4.75
 2 Austria     0.62
 3 Canada      5.31
 4 Denmark     2.71
 5 Finland     2.28
 6 France      1.5 
 7 Germany     5.52
 8 Italy       0.4 
 9 Japan       0.02
10 Korea       0.11   

Like the above, the results are not arranged by the value of Year. How should I fix this? I tried to use rank() but it also only gives alphabetical results.

Da Sul
  • 1
  • 1

1 Answers1

0

You have fallen victim to the intricacies of non-standard evaluation. You need to quote-unquote Year like this:

arrange(!!sym(Year))

In this case when you use arrange it is expecting a column named Year so you have to do something special that tells arrange to use the value assigned to Year.

Not you might be asking: "why doesn't select have this problem?" And the answer is it does behave this way but it also tries to figure out what you meant if you don't "quote-unquote" In fact you should see a warning the first time you do this in your session. In this case the solution is to do something like this:

select(all_of(Year))

Some reading on NSE:

https://www.r-bloggers.com/2019/07/bang-bang-how-to-program-with-dplyr/
https://www.brodrigues.co/blog/2019-06-20-tidy_eval_saga/
http://adv-r.had.co.nz/Computing-on-the-language.html

And some other SO posts on this matter:
How do {{}} double curly brackets work in dplyr?
Turn a {{}} (dplyr double curly braces) interpolation into a string

emilliman5
  • 5,816
  • 3
  • 27
  • 37
  • Thank you so much! It works!!!!! I think you are a genius. How did you learn all of this? – Da Sul Jun 15 '22 at 22:56
  • 1
    I’d suggest using arrange(.data[[Year]]) to avoid dealing with !! sym, ensym etc. that are quite confusing – olivroy Jun 16 '22 at 03:40