R: selecting top n rows using slice(arrange)












0















I'm trying to select the top 6 rows (by value) from a data frame in R using



top6 <- d %>%
filter(ReportingYear==2016) %>%
arrange(desc(total.emission)) %>%
slice(1:6)


but it returns all the rows in d.
I couldn't understand this behaviour until I realised that there was something odd to me about the object d that is at the start of the pipeline.



> class(T4.d)
[1] "grouped_df" "tbl_df" "tbl" "data.frame"


The object d is created using



  d <- facilities %>%
left_join(releases) %>% #get activity id
filter(PollutantName == "Methane (CH4)" & ActivityCode=="5.(d)") %>%
#filter for pollutant and activity
group_by(CountryName,ReportingYear) %>% #group
summarise(total.emission = sum(TotalQuantity)) #summarise


With



> class(facilities)
[1] "data.frame"


Given that facilities is a data frame, I was expecting the class of d to be data frame too. It seems to me that d is of a different class. Can somebody help?



Thanks










share|improve this question




















  • 2





    Check the output after filter

    – akrun
    Nov 19 '18 at 19:53






  • 1





    Can you post a sample of your data?

    – TeeKea
    Nov 19 '18 at 19:58






  • 1





    This question is pretty clear, and I can offer a helpful answer. Please reopen it so I can do that.

    – Ista
    Nov 23 '18 at 19:42
















0















I'm trying to select the top 6 rows (by value) from a data frame in R using



top6 <- d %>%
filter(ReportingYear==2016) %>%
arrange(desc(total.emission)) %>%
slice(1:6)


but it returns all the rows in d.
I couldn't understand this behaviour until I realised that there was something odd to me about the object d that is at the start of the pipeline.



> class(T4.d)
[1] "grouped_df" "tbl_df" "tbl" "data.frame"


The object d is created using



  d <- facilities %>%
left_join(releases) %>% #get activity id
filter(PollutantName == "Methane (CH4)" & ActivityCode=="5.(d)") %>%
#filter for pollutant and activity
group_by(CountryName,ReportingYear) %>% #group
summarise(total.emission = sum(TotalQuantity)) #summarise


With



> class(facilities)
[1] "data.frame"


Given that facilities is a data frame, I was expecting the class of d to be data frame too. It seems to me that d is of a different class. Can somebody help?



Thanks










share|improve this question




















  • 2





    Check the output after filter

    – akrun
    Nov 19 '18 at 19:53






  • 1





    Can you post a sample of your data?

    – TeeKea
    Nov 19 '18 at 19:58






  • 1





    This question is pretty clear, and I can offer a helpful answer. Please reopen it so I can do that.

    – Ista
    Nov 23 '18 at 19:42














0












0








0








I'm trying to select the top 6 rows (by value) from a data frame in R using



top6 <- d %>%
filter(ReportingYear==2016) %>%
arrange(desc(total.emission)) %>%
slice(1:6)


but it returns all the rows in d.
I couldn't understand this behaviour until I realised that there was something odd to me about the object d that is at the start of the pipeline.



> class(T4.d)
[1] "grouped_df" "tbl_df" "tbl" "data.frame"


The object d is created using



  d <- facilities %>%
left_join(releases) %>% #get activity id
filter(PollutantName == "Methane (CH4)" & ActivityCode=="5.(d)") %>%
#filter for pollutant and activity
group_by(CountryName,ReportingYear) %>% #group
summarise(total.emission = sum(TotalQuantity)) #summarise


With



> class(facilities)
[1] "data.frame"


Given that facilities is a data frame, I was expecting the class of d to be data frame too. It seems to me that d is of a different class. Can somebody help?



Thanks










share|improve this question
















I'm trying to select the top 6 rows (by value) from a data frame in R using



top6 <- d %>%
filter(ReportingYear==2016) %>%
arrange(desc(total.emission)) %>%
slice(1:6)


but it returns all the rows in d.
I couldn't understand this behaviour until I realised that there was something odd to me about the object d that is at the start of the pipeline.



> class(T4.d)
[1] "grouped_df" "tbl_df" "tbl" "data.frame"


The object d is created using



  d <- facilities %>%
left_join(releases) %>% #get activity id
filter(PollutantName == "Methane (CH4)" & ActivityCode=="5.(d)") %>%
#filter for pollutant and activity
group_by(CountryName,ReportingYear) %>% #group
summarise(total.emission = sum(TotalQuantity)) #summarise


With



> class(facilities)
[1] "data.frame"


Given that facilities is a data frame, I was expecting the class of d to be data frame too. It seems to me that d is of a different class. Can somebody help?



Thanks







r class dataframe dplyr tidyverse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 19:42









Ista

7,69712426




7,69712426










asked Nov 19 '18 at 19:51









alessandroalessandro

244




244








  • 2





    Check the output after filter

    – akrun
    Nov 19 '18 at 19:53






  • 1





    Can you post a sample of your data?

    – TeeKea
    Nov 19 '18 at 19:58






  • 1





    This question is pretty clear, and I can offer a helpful answer. Please reopen it so I can do that.

    – Ista
    Nov 23 '18 at 19:42














  • 2





    Check the output after filter

    – akrun
    Nov 19 '18 at 19:53






  • 1





    Can you post a sample of your data?

    – TeeKea
    Nov 19 '18 at 19:58






  • 1





    This question is pretty clear, and I can offer a helpful answer. Please reopen it so I can do that.

    – Ista
    Nov 23 '18 at 19:42








2




2





Check the output after filter

– akrun
Nov 19 '18 at 19:53





Check the output after filter

– akrun
Nov 19 '18 at 19:53




1




1





Can you post a sample of your data?

– TeeKea
Nov 19 '18 at 19:58





Can you post a sample of your data?

– TeeKea
Nov 19 '18 at 19:58




1




1





This question is pretty clear, and I can offer a helpful answer. Please reopen it so I can do that.

– Ista
Nov 23 '18 at 19:42





This question is pretty clear, and I can offer a helpful answer. Please reopen it so I can do that.

– Ista
Nov 23 '18 at 19:42












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381683%2fr-selecting-top-n-rows-using-slicearrange%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381683%2fr-selecting-top-n-rows-using-slicearrange%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents