Different Date Formats in R [duplicate]
This question already has an answer here:
Convert dd/mm/yy and dd/mm/yyyy to Dates
6 answers
In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").
Here I'm stuck. How can I get all of them to date format ?
Thank you
r date
marked as duplicate by Henrik
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Convert dd/mm/yy and dd/mm/yyyy to Dates
6 answers
In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").
Here I'm stuck. How can I get all of them to date format ?
Thank you
r date
marked as duplicate by Henrik
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Convert dd/mm/yy and dd/mm/yyyy to Dates
6 answers
In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").
Here I'm stuck. How can I get all of them to date format ?
Thank you
r date
This question already has an answer here:
Convert dd/mm/yy and dd/mm/yyyy to Dates
6 answers
In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").
Here I'm stuck. How can I get all of them to date format ?
Thank you
This question already has an answer here:
Convert dd/mm/yy and dd/mm/yyyy to Dates
6 answers
r date
r date
asked Jan 2 at 16:35
CactusTownCactusTown
1
1
marked as duplicate by Henrik
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Henrik
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
An option would be parse_date_time
from lubridate
which can take multiple Date
formats
library(lubridate)
parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
#[1] "2017-11-14 UTC" "2016-04-14 UTC"
Or with anydate
from anytime
. But, applying anydate
, check whether all the format
s are already present with
library(anytime)
getFormats()
If some format
s are missing, add it with addFormats
addFormats("%d-%b-%y")
and then apply the anydate
on the column/vector of dates
anydate(v1)
#[1] "2017-11-14" "2016-04-14"
data
v1 <- c("14-nov-17", "14APR2016")
1
Thank you so much it worked perfectly ! amazing package.
– CactusTown
Jan 3 at 8:55
@CactusTown Thank you for the comments. You can also check here
– akrun
Jan 3 at 8:56
add a comment |
Another option, if you want to use base R and like regular expressions:
data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
format = "%d-%b-%y")
No, just say no. Never ever use regexps for data parsing when suitable libraries exist.
– Dirk Eddelbuettel
Jan 2 at 16:49
@DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?
– C. Braun
Jan 2 at 17:07
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
An option would be parse_date_time
from lubridate
which can take multiple Date
formats
library(lubridate)
parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
#[1] "2017-11-14 UTC" "2016-04-14 UTC"
Or with anydate
from anytime
. But, applying anydate
, check whether all the format
s are already present with
library(anytime)
getFormats()
If some format
s are missing, add it with addFormats
addFormats("%d-%b-%y")
and then apply the anydate
on the column/vector of dates
anydate(v1)
#[1] "2017-11-14" "2016-04-14"
data
v1 <- c("14-nov-17", "14APR2016")
1
Thank you so much it worked perfectly ! amazing package.
– CactusTown
Jan 3 at 8:55
@CactusTown Thank you for the comments. You can also check here
– akrun
Jan 3 at 8:56
add a comment |
An option would be parse_date_time
from lubridate
which can take multiple Date
formats
library(lubridate)
parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
#[1] "2017-11-14 UTC" "2016-04-14 UTC"
Or with anydate
from anytime
. But, applying anydate
, check whether all the format
s are already present with
library(anytime)
getFormats()
If some format
s are missing, add it with addFormats
addFormats("%d-%b-%y")
and then apply the anydate
on the column/vector of dates
anydate(v1)
#[1] "2017-11-14" "2016-04-14"
data
v1 <- c("14-nov-17", "14APR2016")
1
Thank you so much it worked perfectly ! amazing package.
– CactusTown
Jan 3 at 8:55
@CactusTown Thank you for the comments. You can also check here
– akrun
Jan 3 at 8:56
add a comment |
An option would be parse_date_time
from lubridate
which can take multiple Date
formats
library(lubridate)
parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
#[1] "2017-11-14 UTC" "2016-04-14 UTC"
Or with anydate
from anytime
. But, applying anydate
, check whether all the format
s are already present with
library(anytime)
getFormats()
If some format
s are missing, add it with addFormats
addFormats("%d-%b-%y")
and then apply the anydate
on the column/vector of dates
anydate(v1)
#[1] "2017-11-14" "2016-04-14"
data
v1 <- c("14-nov-17", "14APR2016")
An option would be parse_date_time
from lubridate
which can take multiple Date
formats
library(lubridate)
parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
#[1] "2017-11-14 UTC" "2016-04-14 UTC"
Or with anydate
from anytime
. But, applying anydate
, check whether all the format
s are already present with
library(anytime)
getFormats()
If some format
s are missing, add it with addFormats
addFormats("%d-%b-%y")
and then apply the anydate
on the column/vector of dates
anydate(v1)
#[1] "2017-11-14" "2016-04-14"
data
v1 <- c("14-nov-17", "14APR2016")
answered Jan 2 at 16:37
akrunakrun
417k13205278
417k13205278
1
Thank you so much it worked perfectly ! amazing package.
– CactusTown
Jan 3 at 8:55
@CactusTown Thank you for the comments. You can also check here
– akrun
Jan 3 at 8:56
add a comment |
1
Thank you so much it worked perfectly ! amazing package.
– CactusTown
Jan 3 at 8:55
@CactusTown Thank you for the comments. You can also check here
– akrun
Jan 3 at 8:56
1
1
Thank you so much it worked perfectly ! amazing package.
– CactusTown
Jan 3 at 8:55
Thank you so much it worked perfectly ! amazing package.
– CactusTown
Jan 3 at 8:55
@CactusTown Thank you for the comments. You can also check here
– akrun
Jan 3 at 8:56
@CactusTown Thank you for the comments. You can also check here
– akrun
Jan 3 at 8:56
add a comment |
Another option, if you want to use base R and like regular expressions:
data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
format = "%d-%b-%y")
No, just say no. Never ever use regexps for data parsing when suitable libraries exist.
– Dirk Eddelbuettel
Jan 2 at 16:49
@DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?
– C. Braun
Jan 2 at 17:07
add a comment |
Another option, if you want to use base R and like regular expressions:
data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
format = "%d-%b-%y")
No, just say no. Never ever use regexps for data parsing when suitable libraries exist.
– Dirk Eddelbuettel
Jan 2 at 16:49
@DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?
– C. Braun
Jan 2 at 17:07
add a comment |
Another option, if you want to use base R and like regular expressions:
data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
format = "%d-%b-%y")
Another option, if you want to use base R and like regular expressions:
data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
format = "%d-%b-%y")
answered Jan 2 at 16:44
C. BraunC. Braun
2,802629
2,802629
No, just say no. Never ever use regexps for data parsing when suitable libraries exist.
– Dirk Eddelbuettel
Jan 2 at 16:49
@DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?
– C. Braun
Jan 2 at 17:07
add a comment |
No, just say no. Never ever use regexps for data parsing when suitable libraries exist.
– Dirk Eddelbuettel
Jan 2 at 16:49
@DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?
– C. Braun
Jan 2 at 17:07
No, just say no. Never ever use regexps for data parsing when suitable libraries exist.
– Dirk Eddelbuettel
Jan 2 at 16:49
No, just say no. Never ever use regexps for data parsing when suitable libraries exist.
– Dirk Eddelbuettel
Jan 2 at 16:49
@DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?
– C. Braun
Jan 2 at 17:07
@DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?
– C. Braun
Jan 2 at 17:07
add a comment |