Erase minutes and seconds from character dates in R The Next CEO of Stack OverflowSelect every other element from a vectorIn R, split a character vector by a specific character; save 3rd piece in new vectorHow to Correctly Use Lists in R?Identifying unique terms from list of character vectorsConvert data.frame columns from factors to charactersRemove quotes from a character vector in RReplace specific characters within stringsdata.table vs dplyr: can one do something well the other can't or does poorly?In R, how do I extract minutes and seconds from charactersCalculate character string “days, hours, minutes, seconds” to numeric total daysFrom sessions to minute by minute summaryConvert character vector to a new vector of substrings
Is 'diverse range' a pleonastic phrase?
How to safely derail a train during transit?
Why does standard notation not preserve intervals (visually)
What benefits would be gained by using human laborers instead of drones in deep sea mining?
If the heap is initialized for security, then why is the stack uninitialized?
Which tube will fit a -(700 x 25c) wheel?
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
Which kind of appliances can one connect to electric sockets located in a airplane's toilet?
Written every which way
Anatomically Correct Strange Women In Ponds Distributing Swords
Do I need to enable Dev Hub in my PROD Org?
If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?
What is "(CFMCC)" on an ILS approach chart?
Multiple labels for a single equation
What happens if you roll doubles 3 times then land on "Go to jail?"
Return the Closest Prime Number
Real integral using residue theorem - why doesn't this work?
Unreliable Magic - Is it worth it?
How do I transpose the 1st and -1th levels of an arbitrarily nested array?
"In the right combination" vs "with the right combination"?
Why does the UK parliament need a vote on the political declaration?
How did people program for Consoles with multiple CPUs?
Bold, vivid family
Why do remote companies require working in the US?
Erase minutes and seconds from character dates in R
The Next CEO of Stack OverflowSelect every other element from a vectorIn R, split a character vector by a specific character; save 3rd piece in new vectorHow to Correctly Use Lists in R?Identifying unique terms from list of character vectorsConvert data.frame columns from factors to charactersRemove quotes from a character vector in RReplace specific characters within stringsdata.table vs dplyr: can one do something well the other can't or does poorly?In R, how do I extract minutes and seconds from charactersCalculate character string “days, hours, minutes, seconds” to numeric total daysFrom sessions to minute by minute summaryConvert character vector to a new vector of substrings
I have this timestamp vector:
c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
I'd like to strip off the minutes and seconds from the character vector so that I just have 01/09/2019 9
and 01/09/2019 10
What is the most efficient method to do so?
r
add a comment |
I have this timestamp vector:
c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
I'd like to strip off the minutes and seconds from the character vector so that I just have 01/09/2019 9
and 01/09/2019 10
What is the most efficient method to do so?
r
Interesting accepted answer, what is your definition of efficient?
– Hector Haffenden
Mar 22 at 12:53
I'm pretty biased towardstidyverse
packages :)
– JasonBaik
Mar 22 at 14:40
ah I see, I don’t blame you :) next time you should probably put it in the question...
– Hector Haffenden
Mar 24 at 19:07
add a comment |
I have this timestamp vector:
c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
I'd like to strip off the minutes and seconds from the character vector so that I just have 01/09/2019 9
and 01/09/2019 10
What is the most efficient method to do so?
r
I have this timestamp vector:
c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
I'd like to strip off the minutes and seconds from the character vector so that I just have 01/09/2019 9
and 01/09/2019 10
What is the most efficient method to do so?
r
r
asked Mar 21 at 1:02
JasonBaikJasonBaik
406114
406114
Interesting accepted answer, what is your definition of efficient?
– Hector Haffenden
Mar 22 at 12:53
I'm pretty biased towardstidyverse
packages :)
– JasonBaik
Mar 22 at 14:40
ah I see, I don’t blame you :) next time you should probably put it in the question...
– Hector Haffenden
Mar 24 at 19:07
add a comment |
Interesting accepted answer, what is your definition of efficient?
– Hector Haffenden
Mar 22 at 12:53
I'm pretty biased towardstidyverse
packages :)
– JasonBaik
Mar 22 at 14:40
ah I see, I don’t blame you :) next time you should probably put it in the question...
– Hector Haffenden
Mar 24 at 19:07
Interesting accepted answer, what is your definition of efficient?
– Hector Haffenden
Mar 22 at 12:53
Interesting accepted answer, what is your definition of efficient?
– Hector Haffenden
Mar 22 at 12:53
I'm pretty biased towards
tidyverse
packages :)– JasonBaik
Mar 22 at 14:40
I'm pretty biased towards
tidyverse
packages :)– JasonBaik
Mar 22 at 14:40
ah I see, I don’t blame you :) next time you should probably put it in the question...
– Hector Haffenden
Mar 24 at 19:07
ah I see, I don’t blame you :) next time you should probably put it in the question...
– Hector Haffenden
Mar 24 at 19:07
add a comment |
6 Answers
6
active
oldest
votes
You could also use str_extract
from stringr
:
date_strings <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
str_extract(date_strings, ".+(?=:.+:)")
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10"
[5] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[9] "01/09/2019 10" "01/09/2019 10"
add a comment |
Here's one.
datevec <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
format(as.POSIXct(datevec, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H")
# Result
[1] "01/09/2019 09" "01/09/2019 09" "01/09/2019 09" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[7] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
add a comment |
What is your desired output class? How about this one:
v <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
strptime(v, "%m/%d/%Y %H")
add a comment |
This seems nice,
unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)]
(Made with help from here)
Alternative could be,
sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1)
Using some benchmarks and recent comments by Ronak, that fixed=TRUE makes the methods a lot faster, we see that method four (the above method) is fastest,
mystring <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
microbenchmark(one = sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1),
two = unlist(lapply(mystring,function(x) strsplit(x,":", fixed=TRUE)[[1]][1])),
three = strptime(mystring, "%m/%d/%Y %H"),
four = unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)],
five = format(as.POSIXct(mystring, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H"),
six = gsub("(.*?):.*", "\1", mystring),
seven = str_extract(mystring, ".+(?=:.+:)"),
times = 100000)
Unit: microseconds
expr min lq mean median uq max neval
one 42.792 49.471 85.63742 52.572 57.1310 669280.96 1e+05
two 64.637 70.618 114.16364 73.252 77.6840 582466.94 1e+05
three 129.456 134.771 156.82308 136.188 139.2030 339715.94 1e+05
four 12.860 15.641 22.75699 17.254 18.5440 305703.52 1e+05
five 482.888 505.647 633.15388 512.880 552.1155 551274.28 1e+05
six 37.889 43.121 52.79030 45.567 49.1880 32954.59 1e+05
seven 53.432 59.051 88.05015 62.326 69.9320 1180361.17 1e+05
yep. I thinkfixed = TRUE
makes it a lot faster.
– Ronak Shah
Mar 21 at 1:40
1
Very interesting, actually 4, with fixed=TRUE is fastest, changed the benchmark to show this.
– Hector Haffenden
Mar 21 at 1:43
add a comment |
Another one:
dates <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
unlist(lapply(dates,function(x) strsplit(x,":")[[1]][1]))
gives
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10" "01/09/2019 10"
[6] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
add a comment |
Here is another one using gsub
Capture the pattern by ()
and \1
to refer to the captured group, need ?
to make regex lazy as there are multiple :
.
gsub("(.*?):.*", "\1", dates)
Added to my benchmarking answer, hope thats okay.
– Hector Haffenden
Mar 21 at 2:17
All good, its great to see those benchmarks.
– MKa
Mar 21 at 2:55
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%2f55272307%2ferase-minutes-and-seconds-from-character-dates-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could also use str_extract
from stringr
:
date_strings <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
str_extract(date_strings, ".+(?=:.+:)")
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10"
[5] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[9] "01/09/2019 10" "01/09/2019 10"
add a comment |
You could also use str_extract
from stringr
:
date_strings <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
str_extract(date_strings, ".+(?=:.+:)")
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10"
[5] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[9] "01/09/2019 10" "01/09/2019 10"
add a comment |
You could also use str_extract
from stringr
:
date_strings <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
str_extract(date_strings, ".+(?=:.+:)")
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10"
[5] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[9] "01/09/2019 10" "01/09/2019 10"
You could also use str_extract
from stringr
:
date_strings <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
str_extract(date_strings, ".+(?=:.+:)")
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10"
[5] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[9] "01/09/2019 10" "01/09/2019 10"
answered Mar 21 at 17:06
Ben GBen G
911321
911321
add a comment |
add a comment |
Here's one.
datevec <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
format(as.POSIXct(datevec, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H")
# Result
[1] "01/09/2019 09" "01/09/2019 09" "01/09/2019 09" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[7] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
add a comment |
Here's one.
datevec <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
format(as.POSIXct(datevec, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H")
# Result
[1] "01/09/2019 09" "01/09/2019 09" "01/09/2019 09" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[7] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
add a comment |
Here's one.
datevec <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
format(as.POSIXct(datevec, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H")
# Result
[1] "01/09/2019 09" "01/09/2019 09" "01/09/2019 09" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[7] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
Here's one.
datevec <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
format(as.POSIXct(datevec, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H")
# Result
[1] "01/09/2019 09" "01/09/2019 09" "01/09/2019 09" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
[7] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
edited Mar 21 at 1:32
answered Mar 21 at 1:09
hmhensenhmhensen
7621718
7621718
add a comment |
add a comment |
What is your desired output class? How about this one:
v <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
strptime(v, "%m/%d/%Y %H")
add a comment |
What is your desired output class? How about this one:
v <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
strptime(v, "%m/%d/%Y %H")
add a comment |
What is your desired output class? How about this one:
v <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
strptime(v, "%m/%d/%Y %H")
What is your desired output class? How about this one:
v <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
strptime(v, "%m/%d/%Y %H")
edited Mar 21 at 1:30
answered Mar 21 at 1:13
Ozan147Ozan147
2,0521519
2,0521519
add a comment |
add a comment |
This seems nice,
unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)]
(Made with help from here)
Alternative could be,
sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1)
Using some benchmarks and recent comments by Ronak, that fixed=TRUE makes the methods a lot faster, we see that method four (the above method) is fastest,
mystring <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
microbenchmark(one = sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1),
two = unlist(lapply(mystring,function(x) strsplit(x,":", fixed=TRUE)[[1]][1])),
three = strptime(mystring, "%m/%d/%Y %H"),
four = unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)],
five = format(as.POSIXct(mystring, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H"),
six = gsub("(.*?):.*", "\1", mystring),
seven = str_extract(mystring, ".+(?=:.+:)"),
times = 100000)
Unit: microseconds
expr min lq mean median uq max neval
one 42.792 49.471 85.63742 52.572 57.1310 669280.96 1e+05
two 64.637 70.618 114.16364 73.252 77.6840 582466.94 1e+05
three 129.456 134.771 156.82308 136.188 139.2030 339715.94 1e+05
four 12.860 15.641 22.75699 17.254 18.5440 305703.52 1e+05
five 482.888 505.647 633.15388 512.880 552.1155 551274.28 1e+05
six 37.889 43.121 52.79030 45.567 49.1880 32954.59 1e+05
seven 53.432 59.051 88.05015 62.326 69.9320 1180361.17 1e+05
yep. I thinkfixed = TRUE
makes it a lot faster.
– Ronak Shah
Mar 21 at 1:40
1
Very interesting, actually 4, with fixed=TRUE is fastest, changed the benchmark to show this.
– Hector Haffenden
Mar 21 at 1:43
add a comment |
This seems nice,
unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)]
(Made with help from here)
Alternative could be,
sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1)
Using some benchmarks and recent comments by Ronak, that fixed=TRUE makes the methods a lot faster, we see that method four (the above method) is fastest,
mystring <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
microbenchmark(one = sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1),
two = unlist(lapply(mystring,function(x) strsplit(x,":", fixed=TRUE)[[1]][1])),
three = strptime(mystring, "%m/%d/%Y %H"),
four = unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)],
five = format(as.POSIXct(mystring, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H"),
six = gsub("(.*?):.*", "\1", mystring),
seven = str_extract(mystring, ".+(?=:.+:)"),
times = 100000)
Unit: microseconds
expr min lq mean median uq max neval
one 42.792 49.471 85.63742 52.572 57.1310 669280.96 1e+05
two 64.637 70.618 114.16364 73.252 77.6840 582466.94 1e+05
three 129.456 134.771 156.82308 136.188 139.2030 339715.94 1e+05
four 12.860 15.641 22.75699 17.254 18.5440 305703.52 1e+05
five 482.888 505.647 633.15388 512.880 552.1155 551274.28 1e+05
six 37.889 43.121 52.79030 45.567 49.1880 32954.59 1e+05
seven 53.432 59.051 88.05015 62.326 69.9320 1180361.17 1e+05
yep. I thinkfixed = TRUE
makes it a lot faster.
– Ronak Shah
Mar 21 at 1:40
1
Very interesting, actually 4, with fixed=TRUE is fastest, changed the benchmark to show this.
– Hector Haffenden
Mar 21 at 1:43
add a comment |
This seems nice,
unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)]
(Made with help from here)
Alternative could be,
sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1)
Using some benchmarks and recent comments by Ronak, that fixed=TRUE makes the methods a lot faster, we see that method four (the above method) is fastest,
mystring <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
microbenchmark(one = sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1),
two = unlist(lapply(mystring,function(x) strsplit(x,":", fixed=TRUE)[[1]][1])),
three = strptime(mystring, "%m/%d/%Y %H"),
four = unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)],
five = format(as.POSIXct(mystring, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H"),
six = gsub("(.*?):.*", "\1", mystring),
seven = str_extract(mystring, ".+(?=:.+:)"),
times = 100000)
Unit: microseconds
expr min lq mean median uq max neval
one 42.792 49.471 85.63742 52.572 57.1310 669280.96 1e+05
two 64.637 70.618 114.16364 73.252 77.6840 582466.94 1e+05
three 129.456 134.771 156.82308 136.188 139.2030 339715.94 1e+05
four 12.860 15.641 22.75699 17.254 18.5440 305703.52 1e+05
five 482.888 505.647 633.15388 512.880 552.1155 551274.28 1e+05
six 37.889 43.121 52.79030 45.567 49.1880 32954.59 1e+05
seven 53.432 59.051 88.05015 62.326 69.9320 1180361.17 1e+05
This seems nice,
unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)]
(Made with help from here)
Alternative could be,
sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1)
Using some benchmarks and recent comments by Ronak, that fixed=TRUE makes the methods a lot faster, we see that method four (the above method) is fastest,
mystring <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
microbenchmark(one = sapply(strsplit(mystring, split=':', fixed=TRUE), `[`, 1),
two = unlist(lapply(mystring,function(x) strsplit(x,":", fixed=TRUE)[[1]][1])),
three = strptime(mystring, "%m/%d/%Y %H"),
four = unlist(strsplit(mystring, split = ":", fixed=TRUE))[c(TRUE, FALSE,FALSE)],
five = format(as.POSIXct(mystring, format = "%d/%m/%Y %H:%M:%OS"), "%d/%m/%Y %H"),
six = gsub("(.*?):.*", "\1", mystring),
seven = str_extract(mystring, ".+(?=:.+:)"),
times = 100000)
Unit: microseconds
expr min lq mean median uq max neval
one 42.792 49.471 85.63742 52.572 57.1310 669280.96 1e+05
two 64.637 70.618 114.16364 73.252 77.6840 582466.94 1e+05
three 129.456 134.771 156.82308 136.188 139.2030 339715.94 1e+05
four 12.860 15.641 22.75699 17.254 18.5440 305703.52 1e+05
five 482.888 505.647 633.15388 512.880 552.1155 551274.28 1e+05
six 37.889 43.121 52.79030 45.567 49.1880 32954.59 1e+05
seven 53.432 59.051 88.05015 62.326 69.9320 1180361.17 1e+05
edited Mar 22 at 12:59
answered Mar 21 at 1:17
Hector HaffendenHector Haffenden
501114
501114
yep. I thinkfixed = TRUE
makes it a lot faster.
– Ronak Shah
Mar 21 at 1:40
1
Very interesting, actually 4, with fixed=TRUE is fastest, changed the benchmark to show this.
– Hector Haffenden
Mar 21 at 1:43
add a comment |
yep. I thinkfixed = TRUE
makes it a lot faster.
– Ronak Shah
Mar 21 at 1:40
1
Very interesting, actually 4, with fixed=TRUE is fastest, changed the benchmark to show this.
– Hector Haffenden
Mar 21 at 1:43
yep. I think
fixed = TRUE
makes it a lot faster.– Ronak Shah
Mar 21 at 1:40
yep. I think
fixed = TRUE
makes it a lot faster.– Ronak Shah
Mar 21 at 1:40
1
1
Very interesting, actually 4, with fixed=TRUE is fastest, changed the benchmark to show this.
– Hector Haffenden
Mar 21 at 1:43
Very interesting, actually 4, with fixed=TRUE is fastest, changed the benchmark to show this.
– Hector Haffenden
Mar 21 at 1:43
add a comment |
Another one:
dates <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
unlist(lapply(dates,function(x) strsplit(x,":")[[1]][1]))
gives
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10" "01/09/2019 10"
[6] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
add a comment |
Another one:
dates <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
unlist(lapply(dates,function(x) strsplit(x,":")[[1]][1]))
gives
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10" "01/09/2019 10"
[6] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
add a comment |
Another one:
dates <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
unlist(lapply(dates,function(x) strsplit(x,":")[[1]][1]))
gives
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10" "01/09/2019 10"
[6] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
Another one:
dates <- c("01/09/2019 9:51:03", "01/09/2019 9:51:39", "01/09/2019 9:57:04",
"01/09/2019 10:01:41", "01/09/2019 10:06:06", "01/09/2019 10:09:36",
"01/09/2019 10:11:55", "01/09/2019 10:21:15", "01/09/2019 10:21:39",
"01/09/2019 10:52:20")
unlist(lapply(dates,function(x) strsplit(x,":")[[1]][1]))
gives
[1] "01/09/2019 9" "01/09/2019 9" "01/09/2019 9" "01/09/2019 10" "01/09/2019 10"
[6] "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10" "01/09/2019 10"
answered Mar 21 at 1:16
CIAndrewsCIAndrews
55849
55849
add a comment |
add a comment |
Here is another one using gsub
Capture the pattern by ()
and \1
to refer to the captured group, need ?
to make regex lazy as there are multiple :
.
gsub("(.*?):.*", "\1", dates)
Added to my benchmarking answer, hope thats okay.
– Hector Haffenden
Mar 21 at 2:17
All good, its great to see those benchmarks.
– MKa
Mar 21 at 2:55
add a comment |
Here is another one using gsub
Capture the pattern by ()
and \1
to refer to the captured group, need ?
to make regex lazy as there are multiple :
.
gsub("(.*?):.*", "\1", dates)
Added to my benchmarking answer, hope thats okay.
– Hector Haffenden
Mar 21 at 2:17
All good, its great to see those benchmarks.
– MKa
Mar 21 at 2:55
add a comment |
Here is another one using gsub
Capture the pattern by ()
and \1
to refer to the captured group, need ?
to make regex lazy as there are multiple :
.
gsub("(.*?):.*", "\1", dates)
Here is another one using gsub
Capture the pattern by ()
and \1
to refer to the captured group, need ?
to make regex lazy as there are multiple :
.
gsub("(.*?):.*", "\1", dates)
answered Mar 21 at 1:56
MKaMKa
358412
358412
Added to my benchmarking answer, hope thats okay.
– Hector Haffenden
Mar 21 at 2:17
All good, its great to see those benchmarks.
– MKa
Mar 21 at 2:55
add a comment |
Added to my benchmarking answer, hope thats okay.
– Hector Haffenden
Mar 21 at 2:17
All good, its great to see those benchmarks.
– MKa
Mar 21 at 2:55
Added to my benchmarking answer, hope thats okay.
– Hector Haffenden
Mar 21 at 2:17
Added to my benchmarking answer, hope thats okay.
– Hector Haffenden
Mar 21 at 2:17
All good, its great to see those benchmarks.
– MKa
Mar 21 at 2:55
All good, its great to see those benchmarks.
– MKa
Mar 21 at 2:55
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%2f55272307%2ferase-minutes-and-seconds-from-character-dates-in-r%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
Interesting accepted answer, what is your definition of efficient?
– Hector Haffenden
Mar 22 at 12:53
I'm pretty biased towards
tidyverse
packages :)– JasonBaik
Mar 22 at 14:40
ah I see, I don’t blame you :) next time you should probably put it in the question...
– Hector Haffenden
Mar 24 at 19:07