to subset a dataframe based on year and month
I want to subset my dataframe from Sept 2017 to April 2018. My dataframe is like this:-
Year Month Day Avg_Temp
2017 8 31 20
2017 9 1 22
.
.
.
2018 4 30 26
2018 5 1 30
I want that my dataset from 1 Sept 2017 to 30 April 2018.
Year Month Day Avg_Temp
2017 9 1 22
.
.
.
2018 4 30 26
based on just the year I am to do subset.
df <-df[df$YEAR>="2017" & df$YEAR<="2018", ]
But I need to subset from month as well. Any help would be great
r
add a comment |
I want to subset my dataframe from Sept 2017 to April 2018. My dataframe is like this:-
Year Month Day Avg_Temp
2017 8 31 20
2017 9 1 22
.
.
.
2018 4 30 26
2018 5 1 30
I want that my dataset from 1 Sept 2017 to 30 April 2018.
Year Month Day Avg_Temp
2017 9 1 22
.
.
.
2018 4 30 26
based on just the year I am to do subset.
df <-df[df$YEAR>="2017" & df$YEAR<="2018", ]
But I need to subset from month as well. Any help would be great
r
add a comment |
I want to subset my dataframe from Sept 2017 to April 2018. My dataframe is like this:-
Year Month Day Avg_Temp
2017 8 31 20
2017 9 1 22
.
.
.
2018 4 30 26
2018 5 1 30
I want that my dataset from 1 Sept 2017 to 30 April 2018.
Year Month Day Avg_Temp
2017 9 1 22
.
.
.
2018 4 30 26
based on just the year I am to do subset.
df <-df[df$YEAR>="2017" & df$YEAR<="2018", ]
But I need to subset from month as well. Any help would be great
r
I want to subset my dataframe from Sept 2017 to April 2018. My dataframe is like this:-
Year Month Day Avg_Temp
2017 8 31 20
2017 9 1 22
.
.
.
2018 4 30 26
2018 5 1 30
I want that my dataset from 1 Sept 2017 to 30 April 2018.
Year Month Day Avg_Temp
2017 9 1 22
.
.
.
2018 4 30 26
based on just the year I am to do subset.
df <-df[df$YEAR>="2017" & df$YEAR<="2018", ]
But I need to subset from month as well. Any help would be great
r
r
edited Nov 13 '18 at 1:06
Marius
31.3k97174
31.3k97174
asked Nov 13 '18 at 1:05
supriya singhsupriya singh
234
234
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Here is a dplyr
approach:
require(tidyverse)
df<-data.frame(Year=c(2018,2017,2017,2017,2018,2018,2018),
Month=c(9,8,10,4,9,3,4),Day=c(13,12,14,15,17,15,14))
df %>%
filter(Year==2017&Month>=9|Year==2018&Month<=4)
Which Yields this:
Year Month Day
1 2017 9 14
2 2018 3 15
3 2018 4 14
Thank you for the response. Using this:- RawWeatherBoston %>% filter(YEAR==2017&MONTH%in%c(4:7)), I am able to get data from April 2017 to July 2017. I want to get data from Sept 2017 to April 2018.
– supriya singh
Nov 13 '18 at 1:24
I've updated my answer. See if that works for you. I for some reason took Sept to be 7, my bad!
– NelsonGon
Nov 13 '18 at 1:44
Thanks a lot. It worked :-)
– supriya singh
Nov 13 '18 at 15:06
add a comment |
Try this option:
df <- df[(df$Year == 2017 & df$Month >= 9) |
(df$Year == 2018 & df$Month <= 4), ]
By the way, you might want to consider storing your dates as a proper date type, including a day component.
I tried this but this isn't working. It is not filtering anything. Also the data type of Year is changing to Logical(YEAR : logi TRUE TRUE TRUE TRUE TRUE TRUE)
– supriya singh
Nov 13 '18 at 1:37
@supriyasingh Sorry, I had a typo in there. It works now.
– Tim Biegeleisen
Nov 13 '18 at 1:48
add a comment |
Perhaps it would be easier if the three date components were encoded in one Date
column :
df$Date <- as.Date(paste(df$Year, df$Month, df$Date, sep = '-'))
df$Year <- NULL
df$Month <- NULL
df <- df[df$Date > as.Date('2017-09-01') & df$Date < as.Date('2018-04-01'), ]
1
I don't want to combine all three columns. I need 3 separate columns for Year, Month and Day
– supriya singh
Nov 13 '18 at 1:35
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%2f53272298%2fto-subset-a-dataframe-based-on-year-and-month%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a dplyr
approach:
require(tidyverse)
df<-data.frame(Year=c(2018,2017,2017,2017,2018,2018,2018),
Month=c(9,8,10,4,9,3,4),Day=c(13,12,14,15,17,15,14))
df %>%
filter(Year==2017&Month>=9|Year==2018&Month<=4)
Which Yields this:
Year Month Day
1 2017 9 14
2 2018 3 15
3 2018 4 14
Thank you for the response. Using this:- RawWeatherBoston %>% filter(YEAR==2017&MONTH%in%c(4:7)), I am able to get data from April 2017 to July 2017. I want to get data from Sept 2017 to April 2018.
– supriya singh
Nov 13 '18 at 1:24
I've updated my answer. See if that works for you. I for some reason took Sept to be 7, my bad!
– NelsonGon
Nov 13 '18 at 1:44
Thanks a lot. It worked :-)
– supriya singh
Nov 13 '18 at 15:06
add a comment |
Here is a dplyr
approach:
require(tidyverse)
df<-data.frame(Year=c(2018,2017,2017,2017,2018,2018,2018),
Month=c(9,8,10,4,9,3,4),Day=c(13,12,14,15,17,15,14))
df %>%
filter(Year==2017&Month>=9|Year==2018&Month<=4)
Which Yields this:
Year Month Day
1 2017 9 14
2 2018 3 15
3 2018 4 14
Thank you for the response. Using this:- RawWeatherBoston %>% filter(YEAR==2017&MONTH%in%c(4:7)), I am able to get data from April 2017 to July 2017. I want to get data from Sept 2017 to April 2018.
– supriya singh
Nov 13 '18 at 1:24
I've updated my answer. See if that works for you. I for some reason took Sept to be 7, my bad!
– NelsonGon
Nov 13 '18 at 1:44
Thanks a lot. It worked :-)
– supriya singh
Nov 13 '18 at 15:06
add a comment |
Here is a dplyr
approach:
require(tidyverse)
df<-data.frame(Year=c(2018,2017,2017,2017,2018,2018,2018),
Month=c(9,8,10,4,9,3,4),Day=c(13,12,14,15,17,15,14))
df %>%
filter(Year==2017&Month>=9|Year==2018&Month<=4)
Which Yields this:
Year Month Day
1 2017 9 14
2 2018 3 15
3 2018 4 14
Here is a dplyr
approach:
require(tidyverse)
df<-data.frame(Year=c(2018,2017,2017,2017,2018,2018,2018),
Month=c(9,8,10,4,9,3,4),Day=c(13,12,14,15,17,15,14))
df %>%
filter(Year==2017&Month>=9|Year==2018&Month<=4)
Which Yields this:
Year Month Day
1 2017 9 14
2 2018 3 15
3 2018 4 14
edited Nov 13 '18 at 1:43
answered Nov 13 '18 at 1:12
NelsonGonNelsonGon
905319
905319
Thank you for the response. Using this:- RawWeatherBoston %>% filter(YEAR==2017&MONTH%in%c(4:7)), I am able to get data from April 2017 to July 2017. I want to get data from Sept 2017 to April 2018.
– supriya singh
Nov 13 '18 at 1:24
I've updated my answer. See if that works for you. I for some reason took Sept to be 7, my bad!
– NelsonGon
Nov 13 '18 at 1:44
Thanks a lot. It worked :-)
– supriya singh
Nov 13 '18 at 15:06
add a comment |
Thank you for the response. Using this:- RawWeatherBoston %>% filter(YEAR==2017&MONTH%in%c(4:7)), I am able to get data from April 2017 to July 2017. I want to get data from Sept 2017 to April 2018.
– supriya singh
Nov 13 '18 at 1:24
I've updated my answer. See if that works for you. I for some reason took Sept to be 7, my bad!
– NelsonGon
Nov 13 '18 at 1:44
Thanks a lot. It worked :-)
– supriya singh
Nov 13 '18 at 15:06
Thank you for the response. Using this:- RawWeatherBoston %>% filter(YEAR==2017&MONTH%in%c(4:7)), I am able to get data from April 2017 to July 2017. I want to get data from Sept 2017 to April 2018.
– supriya singh
Nov 13 '18 at 1:24
Thank you for the response. Using this:- RawWeatherBoston %>% filter(YEAR==2017&MONTH%in%c(4:7)), I am able to get data from April 2017 to July 2017. I want to get data from Sept 2017 to April 2018.
– supriya singh
Nov 13 '18 at 1:24
I've updated my answer. See if that works for you. I for some reason took Sept to be 7, my bad!
– NelsonGon
Nov 13 '18 at 1:44
I've updated my answer. See if that works for you. I for some reason took Sept to be 7, my bad!
– NelsonGon
Nov 13 '18 at 1:44
Thanks a lot. It worked :-)
– supriya singh
Nov 13 '18 at 15:06
Thanks a lot. It worked :-)
– supriya singh
Nov 13 '18 at 15:06
add a comment |
Try this option:
df <- df[(df$Year == 2017 & df$Month >= 9) |
(df$Year == 2018 & df$Month <= 4), ]
By the way, you might want to consider storing your dates as a proper date type, including a day component.
I tried this but this isn't working. It is not filtering anything. Also the data type of Year is changing to Logical(YEAR : logi TRUE TRUE TRUE TRUE TRUE TRUE)
– supriya singh
Nov 13 '18 at 1:37
@supriyasingh Sorry, I had a typo in there. It works now.
– Tim Biegeleisen
Nov 13 '18 at 1:48
add a comment |
Try this option:
df <- df[(df$Year == 2017 & df$Month >= 9) |
(df$Year == 2018 & df$Month <= 4), ]
By the way, you might want to consider storing your dates as a proper date type, including a day component.
I tried this but this isn't working. It is not filtering anything. Also the data type of Year is changing to Logical(YEAR : logi TRUE TRUE TRUE TRUE TRUE TRUE)
– supriya singh
Nov 13 '18 at 1:37
@supriyasingh Sorry, I had a typo in there. It works now.
– Tim Biegeleisen
Nov 13 '18 at 1:48
add a comment |
Try this option:
df <- df[(df$Year == 2017 & df$Month >= 9) |
(df$Year == 2018 & df$Month <= 4), ]
By the way, you might want to consider storing your dates as a proper date type, including a day component.
Try this option:
df <- df[(df$Year == 2017 & df$Month >= 9) |
(df$Year == 2018 & df$Month <= 4), ]
By the way, you might want to consider storing your dates as a proper date type, including a day component.
edited Nov 13 '18 at 1:48
answered Nov 13 '18 at 1:09
Tim BiegeleisenTim Biegeleisen
219k1388140
219k1388140
I tried this but this isn't working. It is not filtering anything. Also the data type of Year is changing to Logical(YEAR : logi TRUE TRUE TRUE TRUE TRUE TRUE)
– supriya singh
Nov 13 '18 at 1:37
@supriyasingh Sorry, I had a typo in there. It works now.
– Tim Biegeleisen
Nov 13 '18 at 1:48
add a comment |
I tried this but this isn't working. It is not filtering anything. Also the data type of Year is changing to Logical(YEAR : logi TRUE TRUE TRUE TRUE TRUE TRUE)
– supriya singh
Nov 13 '18 at 1:37
@supriyasingh Sorry, I had a typo in there. It works now.
– Tim Biegeleisen
Nov 13 '18 at 1:48
I tried this but this isn't working. It is not filtering anything. Also the data type of Year is changing to Logical(YEAR : logi TRUE TRUE TRUE TRUE TRUE TRUE)
– supriya singh
Nov 13 '18 at 1:37
I tried this but this isn't working. It is not filtering anything. Also the data type of Year is changing to Logical(YEAR : logi TRUE TRUE TRUE TRUE TRUE TRUE)
– supriya singh
Nov 13 '18 at 1:37
@supriyasingh Sorry, I had a typo in there. It works now.
– Tim Biegeleisen
Nov 13 '18 at 1:48
@supriyasingh Sorry, I had a typo in there. It works now.
– Tim Biegeleisen
Nov 13 '18 at 1:48
add a comment |
Perhaps it would be easier if the three date components were encoded in one Date
column :
df$Date <- as.Date(paste(df$Year, df$Month, df$Date, sep = '-'))
df$Year <- NULL
df$Month <- NULL
df <- df[df$Date > as.Date('2017-09-01') & df$Date < as.Date('2018-04-01'), ]
1
I don't want to combine all three columns. I need 3 separate columns for Year, Month and Day
– supriya singh
Nov 13 '18 at 1:35
add a comment |
Perhaps it would be easier if the three date components were encoded in one Date
column :
df$Date <- as.Date(paste(df$Year, df$Month, df$Date, sep = '-'))
df$Year <- NULL
df$Month <- NULL
df <- df[df$Date > as.Date('2017-09-01') & df$Date < as.Date('2018-04-01'), ]
1
I don't want to combine all three columns. I need 3 separate columns for Year, Month and Day
– supriya singh
Nov 13 '18 at 1:35
add a comment |
Perhaps it would be easier if the three date components were encoded in one Date
column :
df$Date <- as.Date(paste(df$Year, df$Month, df$Date, sep = '-'))
df$Year <- NULL
df$Month <- NULL
df <- df[df$Date > as.Date('2017-09-01') & df$Date < as.Date('2018-04-01'), ]
Perhaps it would be easier if the three date components were encoded in one Date
column :
df$Date <- as.Date(paste(df$Year, df$Month, df$Date, sep = '-'))
df$Year <- NULL
df$Month <- NULL
df <- df[df$Date > as.Date('2017-09-01') & df$Date < as.Date('2018-04-01'), ]
answered Nov 13 '18 at 1:10
12b345b6b7812b345b6b78
767115
767115
1
I don't want to combine all three columns. I need 3 separate columns for Year, Month and Day
– supriya singh
Nov 13 '18 at 1:35
add a comment |
1
I don't want to combine all three columns. I need 3 separate columns for Year, Month and Day
– supriya singh
Nov 13 '18 at 1:35
1
1
I don't want to combine all three columns. I need 3 separate columns for Year, Month and Day
– supriya singh
Nov 13 '18 at 1:35
I don't want to combine all three columns. I need 3 separate columns for Year, Month and Day
– supriya singh
Nov 13 '18 at 1:35
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53272298%2fto-subset-a-dataframe-based-on-year-and-month%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