How to conver daily data over the period of 30 years to Monthly data for a multivariate series?
I have data frame as shown below:
str(Rainfall_Complete)
'data.frame': 8221 obs. of 18 variables:
$ Date : Date, format: "1985-04-29" "1985-04-30" "1985-05-01" ...
$ Month : Ord.factor w/ 12 levels "Jan"<"Feb"<"Mar"<..:
$ Season : Factor w/ 4 levels "Monsoon","PostMonsoon",..:.
$ Year : chr "1985" "1985" "1985" "1985" ...
$ Stn A : num 0 8.8 0 15 26.2 0 2.5 0 0 0 ...
$ Stn B : num 0 0 26 11 13.8 20 0.26 0 0 0 ...
$ Stn C : num 0.1 0 0 0 13.5 27 16 5 0 0 …
I want to convert the above daily time series to monthly time series
I want my data to look something like this
Year Month StnA StnB StnC……..
1985 Jan 150 100 120
1985 Feb 120 98 58
….
2010 Jan 200 100 87
2010 Feb 140 145 120
I tried the following, however it works only for univariate series
library(dplyr)
Monthly_rainfall <- Rainfall_Complete %>% group_by(Year,Month)%>% summarise()
Any help would be appreciated
r dplyr xts lubridate
add a comment |
I have data frame as shown below:
str(Rainfall_Complete)
'data.frame': 8221 obs. of 18 variables:
$ Date : Date, format: "1985-04-29" "1985-04-30" "1985-05-01" ...
$ Month : Ord.factor w/ 12 levels "Jan"<"Feb"<"Mar"<..:
$ Season : Factor w/ 4 levels "Monsoon","PostMonsoon",..:.
$ Year : chr "1985" "1985" "1985" "1985" ...
$ Stn A : num 0 8.8 0 15 26.2 0 2.5 0 0 0 ...
$ Stn B : num 0 0 26 11 13.8 20 0.26 0 0 0 ...
$ Stn C : num 0.1 0 0 0 13.5 27 16 5 0 0 …
I want to convert the above daily time series to monthly time series
I want my data to look something like this
Year Month StnA StnB StnC……..
1985 Jan 150 100 120
1985 Feb 120 98 58
….
2010 Jan 200 100 87
2010 Feb 140 145 120
I tried the following, however it works only for univariate series
library(dplyr)
Monthly_rainfall <- Rainfall_Complete %>% group_by(Year,Month)%>% summarise()
Any help would be appreciated
r dplyr xts lubridate
you need to convert year and month into proper date format 1st,later use your code you will be able to convert it
– Hunaidkhan
Nov 21 '18 at 10:23
If you want to usesummarise
on multiple columns trysummarise_at
. You'll be able to get thesum
of multiple columns at the same time, based on your grouping.
– AntoniosK
Nov 21 '18 at 10:30
Maybepadr::thicken()
could be of help.
– meriops
Nov 21 '18 at 11:16
Please take a look at how to make a reproducible example to make it easier to help
– camille
Nov 21 '18 at 14:46
add a comment |
I have data frame as shown below:
str(Rainfall_Complete)
'data.frame': 8221 obs. of 18 variables:
$ Date : Date, format: "1985-04-29" "1985-04-30" "1985-05-01" ...
$ Month : Ord.factor w/ 12 levels "Jan"<"Feb"<"Mar"<..:
$ Season : Factor w/ 4 levels "Monsoon","PostMonsoon",..:.
$ Year : chr "1985" "1985" "1985" "1985" ...
$ Stn A : num 0 8.8 0 15 26.2 0 2.5 0 0 0 ...
$ Stn B : num 0 0 26 11 13.8 20 0.26 0 0 0 ...
$ Stn C : num 0.1 0 0 0 13.5 27 16 5 0 0 …
I want to convert the above daily time series to monthly time series
I want my data to look something like this
Year Month StnA StnB StnC……..
1985 Jan 150 100 120
1985 Feb 120 98 58
….
2010 Jan 200 100 87
2010 Feb 140 145 120
I tried the following, however it works only for univariate series
library(dplyr)
Monthly_rainfall <- Rainfall_Complete %>% group_by(Year,Month)%>% summarise()
Any help would be appreciated
r dplyr xts lubridate
I have data frame as shown below:
str(Rainfall_Complete)
'data.frame': 8221 obs. of 18 variables:
$ Date : Date, format: "1985-04-29" "1985-04-30" "1985-05-01" ...
$ Month : Ord.factor w/ 12 levels "Jan"<"Feb"<"Mar"<..:
$ Season : Factor w/ 4 levels "Monsoon","PostMonsoon",..:.
$ Year : chr "1985" "1985" "1985" "1985" ...
$ Stn A : num 0 8.8 0 15 26.2 0 2.5 0 0 0 ...
$ Stn B : num 0 0 26 11 13.8 20 0.26 0 0 0 ...
$ Stn C : num 0.1 0 0 0 13.5 27 16 5 0 0 …
I want to convert the above daily time series to monthly time series
I want my data to look something like this
Year Month StnA StnB StnC……..
1985 Jan 150 100 120
1985 Feb 120 98 58
….
2010 Jan 200 100 87
2010 Feb 140 145 120
I tried the following, however it works only for univariate series
library(dplyr)
Monthly_rainfall <- Rainfall_Complete %>% group_by(Year,Month)%>% summarise()
Any help would be appreciated
r dplyr xts lubridate
r dplyr xts lubridate
asked Nov 21 '18 at 10:19
Vasker SharmaVasker Sharma
328
328
you need to convert year and month into proper date format 1st,later use your code you will be able to convert it
– Hunaidkhan
Nov 21 '18 at 10:23
If you want to usesummarise
on multiple columns trysummarise_at
. You'll be able to get thesum
of multiple columns at the same time, based on your grouping.
– AntoniosK
Nov 21 '18 at 10:30
Maybepadr::thicken()
could be of help.
– meriops
Nov 21 '18 at 11:16
Please take a look at how to make a reproducible example to make it easier to help
– camille
Nov 21 '18 at 14:46
add a comment |
you need to convert year and month into proper date format 1st,later use your code you will be able to convert it
– Hunaidkhan
Nov 21 '18 at 10:23
If you want to usesummarise
on multiple columns trysummarise_at
. You'll be able to get thesum
of multiple columns at the same time, based on your grouping.
– AntoniosK
Nov 21 '18 at 10:30
Maybepadr::thicken()
could be of help.
– meriops
Nov 21 '18 at 11:16
Please take a look at how to make a reproducible example to make it easier to help
– camille
Nov 21 '18 at 14:46
you need to convert year and month into proper date format 1st,later use your code you will be able to convert it
– Hunaidkhan
Nov 21 '18 at 10:23
you need to convert year and month into proper date format 1st,later use your code you will be able to convert it
– Hunaidkhan
Nov 21 '18 at 10:23
If you want to use
summarise
on multiple columns try summarise_at
. You'll be able to get the sum
of multiple columns at the same time, based on your grouping.– AntoniosK
Nov 21 '18 at 10:30
If you want to use
summarise
on multiple columns try summarise_at
. You'll be able to get the sum
of multiple columns at the same time, based on your grouping.– AntoniosK
Nov 21 '18 at 10:30
Maybe
padr::thicken()
could be of help.– meriops
Nov 21 '18 at 11:16
Maybe
padr::thicken()
could be of help.– meriops
Nov 21 '18 at 11:16
Please take a look at how to make a reproducible example to make it easier to help
– camille
Nov 21 '18 at 14:46
Please take a look at how to make a reproducible example to make it easier to help
– camille
Nov 21 '18 at 14:46
add a comment |
1 Answer
1
active
oldest
votes
Your attempt uses dplyr
, but the question is tagged with xts
and lubridate
, thus here is a solution using those packages with a reprex.
library(lubridate)
library(xts)
## Create some basic data
ans6 <- xts(anscombe[, 1:6], order.by = as.Date("2018-01-28") + 1:nrow(anscombe))
## Summary by month
mon6 <- apply.monthly(ans6, FUN = mean)
## Re-format
df6 <- as.data.frame(mon6)
df6$year <- year(rownames(df6))
df6$month <- month(rownames(df6), label = TRUE)
df6
## x1 x2 x3 x4 y1 y2 year month
## 2018-01-31 10.33333 10.33333 10.33333 8.000 7.523333 8.673333 2018 Jan
## 2018-02-08 8.50000 8.50000 8.50000 9.375 7.492500 7.061250 2018 Feb
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%2f53409854%2fhow-to-conver-daily-data-over-the-period-of-30-years-to-monthly-data-for-a-multi%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
Your attempt uses dplyr
, but the question is tagged with xts
and lubridate
, thus here is a solution using those packages with a reprex.
library(lubridate)
library(xts)
## Create some basic data
ans6 <- xts(anscombe[, 1:6], order.by = as.Date("2018-01-28") + 1:nrow(anscombe))
## Summary by month
mon6 <- apply.monthly(ans6, FUN = mean)
## Re-format
df6 <- as.data.frame(mon6)
df6$year <- year(rownames(df6))
df6$month <- month(rownames(df6), label = TRUE)
df6
## x1 x2 x3 x4 y1 y2 year month
## 2018-01-31 10.33333 10.33333 10.33333 8.000 7.523333 8.673333 2018 Jan
## 2018-02-08 8.50000 8.50000 8.50000 9.375 7.492500 7.061250 2018 Feb
add a comment |
Your attempt uses dplyr
, but the question is tagged with xts
and lubridate
, thus here is a solution using those packages with a reprex.
library(lubridate)
library(xts)
## Create some basic data
ans6 <- xts(anscombe[, 1:6], order.by = as.Date("2018-01-28") + 1:nrow(anscombe))
## Summary by month
mon6 <- apply.monthly(ans6, FUN = mean)
## Re-format
df6 <- as.data.frame(mon6)
df6$year <- year(rownames(df6))
df6$month <- month(rownames(df6), label = TRUE)
df6
## x1 x2 x3 x4 y1 y2 year month
## 2018-01-31 10.33333 10.33333 10.33333 8.000 7.523333 8.673333 2018 Jan
## 2018-02-08 8.50000 8.50000 8.50000 9.375 7.492500 7.061250 2018 Feb
add a comment |
Your attempt uses dplyr
, but the question is tagged with xts
and lubridate
, thus here is a solution using those packages with a reprex.
library(lubridate)
library(xts)
## Create some basic data
ans6 <- xts(anscombe[, 1:6], order.by = as.Date("2018-01-28") + 1:nrow(anscombe))
## Summary by month
mon6 <- apply.monthly(ans6, FUN = mean)
## Re-format
df6 <- as.data.frame(mon6)
df6$year <- year(rownames(df6))
df6$month <- month(rownames(df6), label = TRUE)
df6
## x1 x2 x3 x4 y1 y2 year month
## 2018-01-31 10.33333 10.33333 10.33333 8.000 7.523333 8.673333 2018 Jan
## 2018-02-08 8.50000 8.50000 8.50000 9.375 7.492500 7.061250 2018 Feb
Your attempt uses dplyr
, but the question is tagged with xts
and lubridate
, thus here is a solution using those packages with a reprex.
library(lubridate)
library(xts)
## Create some basic data
ans6 <- xts(anscombe[, 1:6], order.by = as.Date("2018-01-28") + 1:nrow(anscombe))
## Summary by month
mon6 <- apply.monthly(ans6, FUN = mean)
## Re-format
df6 <- as.data.frame(mon6)
df6$year <- year(rownames(df6))
df6$month <- month(rownames(df6), label = TRUE)
df6
## x1 x2 x3 x4 y1 y2 year month
## 2018-01-31 10.33333 10.33333 10.33333 8.000 7.523333 8.673333 2018 Jan
## 2018-02-08 8.50000 8.50000 8.50000 9.375 7.492500 7.061250 2018 Feb
answered Dec 12 '18 at 0:49
jmuhlenkampjmuhlenkamp
1,452525
1,452525
add a comment |
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%2f53409854%2fhow-to-conver-daily-data-over-the-period-of-30-years-to-monthly-data-for-a-multi%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
you need to convert year and month into proper date format 1st,later use your code you will be able to convert it
– Hunaidkhan
Nov 21 '18 at 10:23
If you want to use
summarise
on multiple columns trysummarise_at
. You'll be able to get thesum
of multiple columns at the same time, based on your grouping.– AntoniosK
Nov 21 '18 at 10:30
Maybe
padr::thicken()
could be of help.– meriops
Nov 21 '18 at 11:16
Please take a look at how to make a reproducible example to make it easier to help
– camille
Nov 21 '18 at 14:46