When Time Series data is offset and incomplete
A new client at work has a fiscal calendar that starts in March and ends in February of the following year:
fiscalMonthLabels <- c("March", "April", "May", "June",
"July", "August", "September", "October",
"November", "December", "January", "February")
But, because they are new, we only have a few months' worth of data:
library(lubridate)
rawDate <- c("2018-09-01", "2018-10-01", "2018-11-01")
actualMonth <- month(rawDate)
newMonth <- rep(0, length(actualMonth))
for (i in 1:length(actualMonth)) {
if (actualMonth[i] == 1) {newMonth[i] <- 11}
else if (actualMonth[i] == 2) {newMonth[i] <- 12}
else {newMonth[i] <- actualMonth[i] - 2}
}
revenue <- c(123, 456, 789)
df <- data.frame(rawDate, actualMonth, newMonth, revenue)
df
rawDate actualMonth newMonth revenue
1 2018-09-01 9 7 123
2 2018-10-01 10 8 456
3 2018-11-01 11 9 789
So when I try to create a new factor with the fiscal month, this is the error I get:
fiscalMonth <- factor(newMonth, labels = fiscalMonthLabels)
Error in factor(newMonth, labels = fiscalMonthLabels) :
invalid 'labels'; length 12 should be 1 or 3
It seems like the factor
command is looking for actualMonth
to contain all twelve possible values in it. How do I get around this issue?
r
add a comment |
A new client at work has a fiscal calendar that starts in March and ends in February of the following year:
fiscalMonthLabels <- c("March", "April", "May", "June",
"July", "August", "September", "October",
"November", "December", "January", "February")
But, because they are new, we only have a few months' worth of data:
library(lubridate)
rawDate <- c("2018-09-01", "2018-10-01", "2018-11-01")
actualMonth <- month(rawDate)
newMonth <- rep(0, length(actualMonth))
for (i in 1:length(actualMonth)) {
if (actualMonth[i] == 1) {newMonth[i] <- 11}
else if (actualMonth[i] == 2) {newMonth[i] <- 12}
else {newMonth[i] <- actualMonth[i] - 2}
}
revenue <- c(123, 456, 789)
df <- data.frame(rawDate, actualMonth, newMonth, revenue)
df
rawDate actualMonth newMonth revenue
1 2018-09-01 9 7 123
2 2018-10-01 10 8 456
3 2018-11-01 11 9 789
So when I try to create a new factor with the fiscal month, this is the error I get:
fiscalMonth <- factor(newMonth, labels = fiscalMonthLabels)
Error in factor(newMonth, labels = fiscalMonthLabels) :
invalid 'labels'; length 12 should be 1 or 3
It seems like the factor
command is looking for actualMonth
to contain all twelve possible values in it. How do I get around this issue?
r
I made an edit, just so it's clear that the new months will be numbered appropriately to reflect the calendar shift.
– mmyoung77
Nov 12 at 16:57
add a comment |
A new client at work has a fiscal calendar that starts in March and ends in February of the following year:
fiscalMonthLabels <- c("March", "April", "May", "June",
"July", "August", "September", "October",
"November", "December", "January", "February")
But, because they are new, we only have a few months' worth of data:
library(lubridate)
rawDate <- c("2018-09-01", "2018-10-01", "2018-11-01")
actualMonth <- month(rawDate)
newMonth <- rep(0, length(actualMonth))
for (i in 1:length(actualMonth)) {
if (actualMonth[i] == 1) {newMonth[i] <- 11}
else if (actualMonth[i] == 2) {newMonth[i] <- 12}
else {newMonth[i] <- actualMonth[i] - 2}
}
revenue <- c(123, 456, 789)
df <- data.frame(rawDate, actualMonth, newMonth, revenue)
df
rawDate actualMonth newMonth revenue
1 2018-09-01 9 7 123
2 2018-10-01 10 8 456
3 2018-11-01 11 9 789
So when I try to create a new factor with the fiscal month, this is the error I get:
fiscalMonth <- factor(newMonth, labels = fiscalMonthLabels)
Error in factor(newMonth, labels = fiscalMonthLabels) :
invalid 'labels'; length 12 should be 1 or 3
It seems like the factor
command is looking for actualMonth
to contain all twelve possible values in it. How do I get around this issue?
r
A new client at work has a fiscal calendar that starts in March and ends in February of the following year:
fiscalMonthLabels <- c("March", "April", "May", "June",
"July", "August", "September", "October",
"November", "December", "January", "February")
But, because they are new, we only have a few months' worth of data:
library(lubridate)
rawDate <- c("2018-09-01", "2018-10-01", "2018-11-01")
actualMonth <- month(rawDate)
newMonth <- rep(0, length(actualMonth))
for (i in 1:length(actualMonth)) {
if (actualMonth[i] == 1) {newMonth[i] <- 11}
else if (actualMonth[i] == 2) {newMonth[i] <- 12}
else {newMonth[i] <- actualMonth[i] - 2}
}
revenue <- c(123, 456, 789)
df <- data.frame(rawDate, actualMonth, newMonth, revenue)
df
rawDate actualMonth newMonth revenue
1 2018-09-01 9 7 123
2 2018-10-01 10 8 456
3 2018-11-01 11 9 789
So when I try to create a new factor with the fiscal month, this is the error I get:
fiscalMonth <- factor(newMonth, labels = fiscalMonthLabels)
Error in factor(newMonth, labels = fiscalMonthLabels) :
invalid 'labels'; length 12 should be 1 or 3
It seems like the factor
command is looking for actualMonth
to contain all twelve possible values in it. How do I get around this issue?
r
r
edited Nov 12 at 16:56
asked Nov 12 at 16:43
mmyoung77
356211
356211
I made an edit, just so it's clear that the new months will be numbered appropriately to reflect the calendar shift.
– mmyoung77
Nov 12 at 16:57
add a comment |
I made an edit, just so it's clear that the new months will be numbered appropriately to reflect the calendar shift.
– mmyoung77
Nov 12 at 16:57
I made an edit, just so it's clear that the new months will be numbered appropriately to reflect the calendar shift.
– mmyoung77
Nov 12 at 16:57
I made an edit, just so it's clear that the new months will be numbered appropriately to reflect the calendar shift.
– mmyoung77
Nov 12 at 16:57
add a comment |
1 Answer
1
active
oldest
votes
You're going to want to assign the levels
too:
fiscalMonth <- factor(actualMonth, levels = 1:12, labels = fiscalMonthLabels)
fiscalMonth
[1] November December January
Levels: March April May June July August September October November December January February
Alternatively, since you're using lubridate::month
, you could just pass the label argument to month, which will return an ordered factor:
fiscalMonth <- month(actualMonth, label = TRUE)
[1] Sep Oct Nov
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
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%2f53266560%2fwhen-time-series-data-is-offset-and-incomplete%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
You're going to want to assign the levels
too:
fiscalMonth <- factor(actualMonth, levels = 1:12, labels = fiscalMonthLabels)
fiscalMonth
[1] November December January
Levels: March April May June July August September October November December January February
Alternatively, since you're using lubridate::month
, you could just pass the label argument to month, which will return an ordered factor:
fiscalMonth <- month(actualMonth, label = TRUE)
[1] Sep Oct Nov
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
add a comment |
You're going to want to assign the levels
too:
fiscalMonth <- factor(actualMonth, levels = 1:12, labels = fiscalMonthLabels)
fiscalMonth
[1] November December January
Levels: March April May June July August September October November December January February
Alternatively, since you're using lubridate::month
, you could just pass the label argument to month, which will return an ordered factor:
fiscalMonth <- month(actualMonth, label = TRUE)
[1] Sep Oct Nov
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
add a comment |
You're going to want to assign the levels
too:
fiscalMonth <- factor(actualMonth, levels = 1:12, labels = fiscalMonthLabels)
fiscalMonth
[1] November December January
Levels: March April May June July August September October November December January February
Alternatively, since you're using lubridate::month
, you could just pass the label argument to month, which will return an ordered factor:
fiscalMonth <- month(actualMonth, label = TRUE)
[1] Sep Oct Nov
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
You're going to want to assign the levels
too:
fiscalMonth <- factor(actualMonth, levels = 1:12, labels = fiscalMonthLabels)
fiscalMonth
[1] November December January
Levels: March April May June July August September October November December January February
Alternatively, since you're using lubridate::month
, you could just pass the label argument to month, which will return an ordered factor:
fiscalMonth <- month(actualMonth, label = TRUE)
[1] Sep Oct Nov
Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < Oct < Nov < Dec
answered Nov 12 at 16:49
JasonAizkalns
10.9k42976
10.9k42976
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.
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%2f53266560%2fwhen-time-series-data-is-offset-and-incomplete%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
I made an edit, just so it's clear that the new months will be numbered appropriately to reflect the calendar shift.
– mmyoung77
Nov 12 at 16:57