top_n() returning all the rows in R

Below is the head of my tibble . I am trying to find the top two countries with the highest r.squared using top_n() command. Why is that I am getting back the whole dataframe instead of just 2 rows? Appreciate inputs.

head(model_p)

country

r.squared

Algeria 0.9522064
Argentina 0.9843108
Australia 0.9830777
Austria 0.9866741
Bangladesh 0.9485248
Belgium 0.9902805

dim(model_p)
[1] 77  2
model_p %>% top_n(n=2, wt=r.squared)

country

r.squared

Algeria 0.952206405
Argentina 0.984310769
Australia 0.983077726
Austria 0.986674082
Bangladesh 0.948524805
Belgium 0.990280511
Benin 0.963144992
Bolivia 0.992357210
Botswana 0.013649835
Brazil 0.994024334
...
1-10 of 77 rows

maybe your rsession got in a bad state,
in which case I would restart.

I did that a few times. Same result.

I can't reproduce your issue.
Have you tried using top_n on an inbuilt dataset, like iris.?

Hello!

Could you show us the output from str(model_p)? Also, since your data is small, paste dput(model_p) in the output. Head() and dim () are good starts, but str() will give more information about your data.

str(model_p)

grouped_df [77 x 2] (S3: grouped_df/tbl_df/tbl/data.frame)
country : Factor w/ 185 levels "Albania","Algeria",..: 2 5 8 9 13 16 18 20 22 23 ... r.squared: num [1:77] 0.952 0.984 0.983 0.987 0.949 ...

  • attr(, "groups")= tibble [77 x 2] (S3: tbl_df/tbl/data.frame)
    .. country: Factor w/ 185 levels "Albania","Algeria",..: 2 5 8 9 13 16 18 20 22 23 ... .. .rows : list [1:77]
    .. .. : int 1 .. .. : int 2
    .. .. : int 3 .. .. : int 4
    .. .. : int 5 .. .. : int 6
    .. .. : int 7 .. .. : int 8
    .. .. : int 9 .. .. : int 10
    .. .. : int 11 .. .. : int 12
    .. .. : int 13 .. .. : int 14
    .. .. : int 15 .. .. : int 16
    .. .. : int 17 .. .. : int 18
    .. .. : int 19 .. .. : int 20
    .. .. : int 21 .. .. : int 22
    .. .. : int 23 .. .. : int 24
    .. .. : int 25 .. .. : int 26
    .. .. : int 27 .. .. : int 28
    .. .. : int 29 .. .. : int 30
    .. .. : int 31 .. .. : int 32
    .. .. : int 33 .. .. : int 34
    .. .. : int 35 .. .. : int 36
    .. .. : int 37 .. .. : int 38
    .. .. : int 39 .. .. : int 40
    .. .. : int 41 .. .. : int 42
    .. .. : int 43 .. .. : int 44
    .. .. : int 45 .. .. : int 46
    .. .. : int 47 .. .. : int 48
    .. .. : int 49 .. .. : int 50
    .. .. : int 51 .. .. : int 52
    .. .. : int 53 .. .. : int 54
    .. .. : int 55 .. .. : int 56
    .. .. : int 57 .. .. : int 58
    .. .. : int 59 .. .. : int 60
    .. .. : int 61 .. .. : int 62
    .. .. : int 63 .. .. : int 64
    .. .. : int 65 .. .. : int 66
    .. .. : int 67 .. .. : int 68
    .. .. : int 69 .. .. : int 70
    .. .. : int 71 .. .. : int 72
    .. .. : int 73 .. .. : int 74
    .. .. : int 75 .. .. : int 76
    .. ..$ : int 77
    .. ..@ ptype: int(0)
    ..- attr(
    , ".drop")= logi TRUE
dput(model_p)

structure(list(country = structure(c(2L, 5L, 8L, 9L, 13L, 16L,
18L, 20L, 22L, 23L, 26L, 27L, 29L, 30L, 32L, 34L, 36L, 39L, 40L,
41L, 46L, 48L, 49L, 50L, 51L, 56L, 57L, 58L, 64L, 65L, 68L, 73L,
76L, 77L, 78L, 83L, 85L, 88L, 95L, 96L, 103L, 104L, 108L, 110L,
115L, 118L, 119L, 122L, 125L, 126L, 127L, 128L, 129L, 130L, 131L,
132L, 134L, 139L, 142L, 144L, 145L, 146L, 151L, 152L, 153L, 156L,
159L, 164L, 166L, 168L, 170L, 175L, 176L, 177L, 180L, 184L, 185L
), .Label = c("Albania", "Algeria", "Angola", "Antigua and Barbuda",
"Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
"Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
"Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
"Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic",
"Chad", "Chile", "China", "Colombia", "Comoros", "Congo, Dem. Rep.",
"Congo, Rep.", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba",
"Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominican Republic",
"Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
"Estonia", "Ethiopia", "Fiji", "Finland", "France", "French Polynesia",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Greenland",
"Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
"Haiti", "Honduras", "Hong Kong, China", "Hungary", "Iceland",
"India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
"Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati",
"Kuwait", "Kyrgyz Republic", "Lao", "Latvia", "Lebanon", "Lesotho",
"Liberia", "Libya", "Lithuania", "Luxembourg", "Macao, China",
"Macedonia, FYR", "Madagascar", "Malawi", "Malaysia", "Maldives",
"Mali", "Malta", "Mauritania", "Mauritius", "Mexico", "Micronesia, Fed. Sts.",
"Moldova", "Mongolia", "Montenegro", "Morocco", "Mozambique",
"Namibia", "Nepal", "Netherlands", "New Caledonia", "New Zealand",
"Nicaragua", "Niger", "Nigeria", "Norway", "Oman", "Pakistan",
"Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
"Poland", "Portugal", "Puerto Rico", "Qatar", "Romania", "Russia",
"Rwanda", "Samoa", "Saudi Arabia", "Senegal", "Serbia", "Seychelles",
"Sierra Leone", "Singapore", "Slovak Republic", "Slovenia", "Solomon Islands",
"South Africa", "South Korea", "Spain", "Sri Lanka", "St. Lucia",
"St. Vincent and the Grenadines", "Sudan", "Suriname", "Swaziland",
"Sweden", "Switzerland", "Syria", "Tajikistan", "Tanzania", "Thailand",
"Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia",
"Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates",
"United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu",
"Venezuela", "Vietnam", "West Bank and Gaza", "Yemen", "Zambia",
"Zimbabwe"), class = "factor"), r.squared = c(0.952206405132762,
0.984310769214998, 0.983077726081577, 0.986674081885558, 0.948524805003509,
0.990280510790692, 0.96314499182456, 0.992357210292538, 0.0136498348502071,
0.994024334167962, 0.949621747898243, 0.694288857285042, 0.628716115385511,
0.995082105782108, 0.23479340949334, 0.965200167764429, 0.968740447252198,
0.353362635873773, 0.930933431582171, 0.676989961200411, 0.93932185727259,
0.924651603417629, 0.960824118061419, 0.969603454129696, 0.950442103577446,
0.817863987207643, 0.989571544890154, 0.994664500281546, 0.981611432049193,
0.989651765537058, 0.980682830592941, 0.893474602632304, 0.973144683725378,
0.96586041988086, 0.940841093114749, 0.996818306879507, 0.967327010461329,
0.415815699163487, 0.00295583213227903, 0.978225542014519, 0.773858532415654,
0.944581809238676, 0.99551429533062, 0.969077990316906, 0.978279559647342,
0.992263690820837, 0.973923825403985, 0.948303432573391, 0.967528103385891,
0.930439168169492, 0.888066357255274, 0.93032701615361, 0.881465100361972,
0.855441183382078, 0.993539510809514, 0.962921697965437, 0.981980560621835,
0.311034350275703, 0.953228905965889, 0.931749217657187, 0.686807194109785,
0.996383843428018, 0.988426772636489, 0.985801418166835, 0.921534604161422,
0.983092302074904, 0.990720830319739, 0.948116725798722, 0.810493831825373,
0.966113425249462, 0.99391413430321, 0.986323541675603, 0.977877414311006,
0.98336080872972, 0.913133351425334, 0.1832351903099, 0.223919512510944
)), row.names = c(NA, -77L), groups = structure(list(country = structure(c(2L,
5L, 8L, 9L, 13L, 16L, 18L, 20L, 22L, 23L, 26L, 27L, 29L, 30L,
32L, 34L, 36L, 39L, 40L, 41L, 46L, 48L, 49L, 50L, 51L, 56L, 57L,
58L, 64L, 65L, 68L, 73L, 76L, 77L, 78L, 83L, 85L, 88L, 95L, 96L,
103L, 104L, 108L, 110L, 115L, 118L, 119L, 122L, 125L, 126L, 127L,
128L, 129L, 130L, 131L, 132L, 134L, 139L, 142L, 144L, 145L, 146L,
151L, 152L, 153L, 156L, 159L, 164L, 166L, 168L, 170L, 175L, 176L,
177L, 180L, 184L, 185L), .Label = c("Albania", "Algeria", "Angola",
"Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia",
"Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh",
"Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan",
"Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei",
"Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon",
"Canada", "Cape Verde", "Central African Republic", "Chad", "Chile",
"China", "Colombia", "Comoros", "Congo, Dem. Rep.", "Congo, Rep.",
"Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic",
"Denmark", "Djibouti", "Dominican Republic", "Ecuador", "Egypt",
"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
"Fiji", "Finland", "France", "French Polynesia", "Gabon", "Gambia",
"Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada",
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras",
"Hong Kong, China", "Hungary", "Iceland", "India", "Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan",
"Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyz Republic",
"Lao", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania",
"Luxembourg", "Macao, China", "Macedonia, FYR", "Madagascar",
"Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Mauritania",
"Mauritius", "Mexico", "Micronesia, Fed. Sts.", "Moldova", "Mongolia",
"Montenegro", "Morocco", "Mozambique", "Namibia", "Nepal", "Netherlands",
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria",
"Norway", "Oman", "Pakistan", "Panama", "Papua New Guinea", "Paraguay",
"Peru", "Philippines", "Poland", "Portugal", "Puerto Rico", "Qatar",
"Romania", "Russia", "Rwanda", "Samoa", "Saudi Arabia", "Senegal",
"Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovak Republic",
"Slovenia", "Solomon Islands", "South Africa", "South Korea",
"Spain", "Sri Lanka", "St. Lucia", "St. Vincent and the Grenadines",
"Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria",
"Tajikistan", "Tanzania", "Thailand", "Timor-Leste", "Togo",
"Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom",
"United States", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela",
"Vietnam", "West Bank and Gaza", "Yemen", "Zambia", "Zimbabwe"
), class = "factor"), .rows = structure(list(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L,
66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -77L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))

Hello,

So I put the results of your dput() into the function data.frame() and this is what my str() returned. Do you notice a difference from your str()? My data.frame is not a grouped_df. Because your data frame is grouped by the country, the top_n() of the country is its own row, since there is only 1 row, even though you requested the top 2. I love str()! Solves lots of issues right away.

> str(topdta)
'data.frame':	77 obs. of  2 variables:
 $ country  : Factor w/ 185 levels "Albania","Algeria",..: 2 5 8 9 13 16 18 20 22 23 ...
 $ r.squared: num  0.952 0.984 0.983 0.987 0.949 ...

Here is the result on the ungrouped data frame:

> topdta %>% top_n(n=2, wt=r.squared)
    country r.squared
1     Italy 0.9968183
2 Singapore 0.9963838
> topdta %>% top_n(n=2, wt=r.squared) %>% nrow()
[1] 2

Just an FYI: It looks like top_n() is not being develop going forward and is replaced by slice_min() and slice_max().

2 Likes

@ fredoxvii, wonderful. Thank you very much. Understood what was the problem. I just ungrouped my dataframe , which was grouped for an earlier analysis. Now, everything is fine. Also, thanks on the information related to slice_* function.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.