Converting characters to dates
In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.
When I try to convert it to a date, I get this error:
> headlines$Date <- as.Date(headlines$Date)
Error in charToDate(x) :
character string is not in a standard unambiguous format
I've also tried the below, but get all NA
s:
> headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")
How should I convert this column to dates?
r date
add a comment |
In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.
When I try to convert it to a date, I get this error:
> headlines$Date <- as.Date(headlines$Date)
Error in charToDate(x) :
character string is not in a standard unambiguous format
I've also tried the below, but get all NA
s:
> headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")
How should I convert this column to dates?
r date
add a comment |
In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.
When I try to convert it to a date, I get this error:
> headlines$Date <- as.Date(headlines$Date)
Error in charToDate(x) :
character string is not in a standard unambiguous format
I've also tried the below, but get all NA
s:
> headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")
How should I convert this column to dates?
r date
In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.
When I try to convert it to a date, I get this error:
> headlines$Date <- as.Date(headlines$Date)
Error in charToDate(x) :
character string is not in a standard unambiguous format
I've also tried the below, but get all NA
s:
> headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")
How should I convert this column to dates?
r date
r date
asked Nov 15 '18 at 1:12
Adam_GAdam_G
2,302114898
2,302114898
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.
In this case I think you just need to fix what you're passing to the format argument. %b
is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m
instead for months stored as numbers. Try this:
headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")
See this page for more info about date formats in R.
add a comment |
Replace format = "%b/%d/%y"
with format = "%m/%d/%y"
.
%b
means month as in Jan
, Feb
, Mar
and so on.
%m
is the integer equivalent (1
, 2
, 3
etc).
Further reading: https://www.stat.berkeley.edu/~s133/dates.html
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%2f53311069%2fconverting-characters-to-dates%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.
In this case I think you just need to fix what you're passing to the format argument. %b
is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m
instead for months stored as numbers. Try this:
headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")
See this page for more info about date formats in R.
add a comment |
You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.
In this case I think you just need to fix what you're passing to the format argument. %b
is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m
instead for months stored as numbers. Try this:
headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")
See this page for more info about date formats in R.
add a comment |
You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.
In this case I think you just need to fix what you're passing to the format argument. %b
is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m
instead for months stored as numbers. Try this:
headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")
See this page for more info about date formats in R.
You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.
In this case I think you just need to fix what you're passing to the format argument. %b
is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m
instead for months stored as numbers. Try this:
headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")
See this page for more info about date formats in R.
answered Nov 15 '18 at 1:34
Boops BoopsBoops Boops
15114
15114
add a comment |
add a comment |
Replace format = "%b/%d/%y"
with format = "%m/%d/%y"
.
%b
means month as in Jan
, Feb
, Mar
and so on.
%m
is the integer equivalent (1
, 2
, 3
etc).
Further reading: https://www.stat.berkeley.edu/~s133/dates.html
add a comment |
Replace format = "%b/%d/%y"
with format = "%m/%d/%y"
.
%b
means month as in Jan
, Feb
, Mar
and so on.
%m
is the integer equivalent (1
, 2
, 3
etc).
Further reading: https://www.stat.berkeley.edu/~s133/dates.html
add a comment |
Replace format = "%b/%d/%y"
with format = "%m/%d/%y"
.
%b
means month as in Jan
, Feb
, Mar
and so on.
%m
is the integer equivalent (1
, 2
, 3
etc).
Further reading: https://www.stat.berkeley.edu/~s133/dates.html
Replace format = "%b/%d/%y"
with format = "%m/%d/%y"
.
%b
means month as in Jan
, Feb
, Mar
and so on.
%m
is the integer equivalent (1
, 2
, 3
etc).
Further reading: https://www.stat.berkeley.edu/~s133/dates.html
answered Nov 15 '18 at 1:17
12b345b6b7812b345b6b78
792116
792116
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%2f53311069%2fconverting-characters-to-dates%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