How to filter data by many years
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
add a comment |
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
?betweenand/orfiltercan 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
add a comment |
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
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
r
edited Nov 25 '18 at 21:13
Ben Bolker
135k13228320
135k13228320
asked Nov 25 '18 at 20:31
S. C.S. C.
32
32
?betweenand/orfiltercan 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
add a comment |
?betweenand/orfiltercan 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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
?betweenand/orfiltercan 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