How to filter data by many years












-2















So I have a data that has many rows of country, year, and number of reported cases of cholera of that year. How should I go about filtering the data in ranges of years?



So I want data between 2008 - 2016 for example. For all I know, filter(__) seems only to do one year.



Also, how should I go about filtering for certain names in variables? So lets say I have a few rows that say X, Y, Z, C, D. How do I only filter for rows of "X" "Y" Z" for example?










share|improve this question

























  • ?between and/or filter can take a complex expression (i.e. >= <=)

    – hrbrmstr
    Nov 25 '18 at 20:34











  • Welcome to Stack Overflow. Please give more context. It's not even clear what language you're talking about. Also, please add several tags that can help others find your question.

    – ba_ul
    Nov 25 '18 at 20:44
















-2















So I have a data that has many rows of country, year, and number of reported cases of cholera of that year. How should I go about filtering the data in ranges of years?



So I want data between 2008 - 2016 for example. For all I know, filter(__) seems only to do one year.



Also, how should I go about filtering for certain names in variables? So lets say I have a few rows that say X, Y, Z, C, D. How do I only filter for rows of "X" "Y" Z" for example?










share|improve this question

























  • ?between and/or filter can take a complex expression (i.e. >= <=)

    – hrbrmstr
    Nov 25 '18 at 20:34











  • Welcome to Stack Overflow. Please give more context. It's not even clear what language you're talking about. Also, please add several tags that can help others find your question.

    – ba_ul
    Nov 25 '18 at 20:44














-2












-2








-2








So I have a data that has many rows of country, year, and number of reported cases of cholera of that year. How should I go about filtering the data in ranges of years?



So I want data between 2008 - 2016 for example. For all I know, filter(__) seems only to do one year.



Also, how should I go about filtering for certain names in variables? So lets say I have a few rows that say X, Y, Z, C, D. How do I only filter for rows of "X" "Y" Z" for example?










share|improve this question
















So I have a data that has many rows of country, year, and number of reported cases of cholera of that year. How should I go about filtering the data in ranges of years?



So I want data between 2008 - 2016 for example. For all I know, filter(__) seems only to do one year.



Also, how should I go about filtering for certain names in variables? So lets say I have a few rows that say X, Y, Z, C, D. How do I only filter for rows of "X" "Y" Z" for example?







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 21:13









Ben Bolker

135k13228320




135k13228320










asked Nov 25 '18 at 20:31









S. C.S. C.

32




32













  • ?between and/or filter can take a complex expression (i.e. >= <=)

    – hrbrmstr
    Nov 25 '18 at 20:34











  • Welcome to Stack Overflow. Please give more context. It's not even clear what language you're talking about. Also, please add several tags that can help others find your question.

    – ba_ul
    Nov 25 '18 at 20:44



















  • ?between and/or filter can take a complex expression (i.e. >= <=)

    – hrbrmstr
    Nov 25 '18 at 20:34











  • Welcome to Stack Overflow. Please give more context. It's not even clear what language you're talking about. Also, please add several tags that can help others find your question.

    – ba_ul
    Nov 25 '18 at 20:44

















?between and/or filter can take a complex expression (i.e. >= <=)

– hrbrmstr
Nov 25 '18 at 20:34





?between and/or filter can take a complex expression (i.e. >= <=)

– hrbrmstr
Nov 25 '18 at 20:34













Welcome to Stack Overflow. Please give more context. It's not even clear what language you're talking about. Also, please add several tags that can help others find your question.

– ba_ul
Nov 25 '18 at 20:44





Welcome to Stack Overflow. Please give more context. It's not even clear what language you're talking about. Also, please add several tags that can help others find your question.

– ba_ul
Nov 25 '18 at 20:44












1 Answer
1






active

oldest

votes


















0














between() and %in% are the operators you're looking for. To filter on both of your criteria simultaneously:



library(dplyr)
filtered_data <- (your_data %>%
filter(between(year,2008,2016),
interesting_row %in% c("X","Y","Z"))
)


Note that between(year,2008,2016) is (nearly) equivalent to 2008 <= year & year <= 2016






share|improve this answer
























  • awesome, thank you!!!

    – S. C.
    Nov 26 '18 at 2:11











  • Although the sentiment is appreciated, StackOverflow deprecates "thank you" comments; instead, if the answer solves your problem you are encouraged to click the check-mark.

    – Ben Bolker
    Nov 26 '18 at 13:24











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%2f53471643%2fhow-to-filter-data-by-many-years%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














between() and %in% are the operators you're looking for. To filter on both of your criteria simultaneously:



library(dplyr)
filtered_data <- (your_data %>%
filter(between(year,2008,2016),
interesting_row %in% c("X","Y","Z"))
)


Note that between(year,2008,2016) is (nearly) equivalent to 2008 <= year & year <= 2016






share|improve this answer
























  • awesome, thank you!!!

    – S. C.
    Nov 26 '18 at 2:11











  • Although the sentiment is appreciated, StackOverflow deprecates "thank you" comments; instead, if the answer solves your problem you are encouraged to click the check-mark.

    – Ben Bolker
    Nov 26 '18 at 13:24
















0














between() and %in% are the operators you're looking for. To filter on both of your criteria simultaneously:



library(dplyr)
filtered_data <- (your_data %>%
filter(between(year,2008,2016),
interesting_row %in% c("X","Y","Z"))
)


Note that between(year,2008,2016) is (nearly) equivalent to 2008 <= year & year <= 2016






share|improve this answer
























  • awesome, thank you!!!

    – S. C.
    Nov 26 '18 at 2:11











  • Although the sentiment is appreciated, StackOverflow deprecates "thank you" comments; instead, if the answer solves your problem you are encouraged to click the check-mark.

    – Ben Bolker
    Nov 26 '18 at 13:24














0












0








0







between() and %in% are the operators you're looking for. To filter on both of your criteria simultaneously:



library(dplyr)
filtered_data <- (your_data %>%
filter(between(year,2008,2016),
interesting_row %in% c("X","Y","Z"))
)


Note that between(year,2008,2016) is (nearly) equivalent to 2008 <= year & year <= 2016






share|improve this answer













between() and %in% are the operators you're looking for. To filter on both of your criteria simultaneously:



library(dplyr)
filtered_data <- (your_data %>%
filter(between(year,2008,2016),
interesting_row %in% c("X","Y","Z"))
)


Note that between(year,2008,2016) is (nearly) equivalent to 2008 <= year & year <= 2016







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 21:16









Ben BolkerBen Bolker

135k13228320




135k13228320













  • awesome, thank you!!!

    – S. C.
    Nov 26 '18 at 2:11











  • Although the sentiment is appreciated, StackOverflow deprecates "thank you" comments; instead, if the answer solves your problem you are encouraged to click the check-mark.

    – Ben Bolker
    Nov 26 '18 at 13:24



















  • awesome, thank you!!!

    – S. C.
    Nov 26 '18 at 2:11











  • Although the sentiment is appreciated, StackOverflow deprecates "thank you" comments; instead, if the answer solves your problem you are encouraged to click the check-mark.

    – Ben Bolker
    Nov 26 '18 at 13:24

















awesome, thank you!!!

– S. C.
Nov 26 '18 at 2:11





awesome, thank you!!!

– S. C.
Nov 26 '18 at 2:11













Although the sentiment is appreciated, StackOverflow deprecates "thank you" comments; instead, if the answer solves your problem you are encouraged to click the check-mark.

– Ben Bolker
Nov 26 '18 at 13:24





Although the sentiment is appreciated, StackOverflow deprecates "thank you" comments; instead, if the answer solves your problem you are encouraged to click the check-mark.

– Ben Bolker
Nov 26 '18 at 13:24




















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%2f53471643%2fhow-to-filter-data-by-many-years%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

Ottavio Pratesi

Tricia Helfer

15 giugno