How do I convert characters to a time that has fractional seconds without adding a date?
Is there a way to convert a string of characters to a time that retains the fractional seconds, but does not add a date to the data?
Background: Data that I saved overnight (starting at 22:00 and ending at 06:00) is recorded with a time, but not a date.
I would like to be able to perform something like an "if" statement on the times (i.e. if (time < midnight) date = yesterday.... else date = today)
I don't want to use something like strptime, because strptime adds today's date to the data:
> options(digits.secs=3)
> strptime("22:59:54.807", format="%H:%M:%OS")
[1] "2019-01-20 22:59:54.807 AEDT"
When I use times from the chron package, the fractional seconds are dropped:
> options(digits.secs=3)
> times("22:59:54.807")
[1] 22:59:55
r
add a comment |
Is there a way to convert a string of characters to a time that retains the fractional seconds, but does not add a date to the data?
Background: Data that I saved overnight (starting at 22:00 and ending at 06:00) is recorded with a time, but not a date.
I would like to be able to perform something like an "if" statement on the times (i.e. if (time < midnight) date = yesterday.... else date = today)
I don't want to use something like strptime, because strptime adds today's date to the data:
> options(digits.secs=3)
> strptime("22:59:54.807", format="%H:%M:%OS")
[1] "2019-01-20 22:59:54.807 AEDT"
When I use times from the chron package, the fractional seconds are dropped:
> options(digits.secs=3)
> times("22:59:54.807")
[1] 22:59:55
r
lubridate package in R is your best bet!
– Data Science
Jan 19 at 21:18
add a comment |
Is there a way to convert a string of characters to a time that retains the fractional seconds, but does not add a date to the data?
Background: Data that I saved overnight (starting at 22:00 and ending at 06:00) is recorded with a time, but not a date.
I would like to be able to perform something like an "if" statement on the times (i.e. if (time < midnight) date = yesterday.... else date = today)
I don't want to use something like strptime, because strptime adds today's date to the data:
> options(digits.secs=3)
> strptime("22:59:54.807", format="%H:%M:%OS")
[1] "2019-01-20 22:59:54.807 AEDT"
When I use times from the chron package, the fractional seconds are dropped:
> options(digits.secs=3)
> times("22:59:54.807")
[1] 22:59:55
r
Is there a way to convert a string of characters to a time that retains the fractional seconds, but does not add a date to the data?
Background: Data that I saved overnight (starting at 22:00 and ending at 06:00) is recorded with a time, but not a date.
I would like to be able to perform something like an "if" statement on the times (i.e. if (time < midnight) date = yesterday.... else date = today)
I don't want to use something like strptime, because strptime adds today's date to the data:
> options(digits.secs=3)
> strptime("22:59:54.807", format="%H:%M:%OS")
[1] "2019-01-20 22:59:54.807 AEDT"
When I use times from the chron package, the fractional seconds are dropped:
> options(digits.secs=3)
> times("22:59:54.807")
[1] 22:59:55
r
r
asked Jan 19 at 20:54
talikarngtalikarng
1
1
lubridate package in R is your best bet!
– Data Science
Jan 19 at 21:18
add a comment |
lubridate package in R is your best bet!
– Data Science
Jan 19 at 21:18
lubridate package in R is your best bet!
– Data Science
Jan 19 at 21:18
lubridate package in R is your best bet!
– Data Science
Jan 19 at 21:18
add a comment |
1 Answer
1
active
oldest
votes
Using base::strftime(), you can obtain a character of the desired time to any divide of seconds you want. Below I have saved the current time to unit 1/1000 of a second, using the number "4" after "OS":
Using current system time:
time = Sys.time()
str(time)
POSIXct[1:1], format: "2019-01-19 15:41:28.185"
newTime = strftime(Sys.time(), format="%H:%M:%OS4")
str(newTime)
chr "15:41:28.1851"
newTime
[1] "15:41:28.1851"
Thanks @JessicaBurnett, once I have the 'chr "15:41:28.1851"', how do you convert that back to a "time" that can be used in a conditional statement such as an "if" statement?
– talikarng
Jan 19 at 23:27
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%2f54271311%2fhow-do-i-convert-characters-to-a-time-that-has-fractional-seconds-without-adding%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
Using base::strftime(), you can obtain a character of the desired time to any divide of seconds you want. Below I have saved the current time to unit 1/1000 of a second, using the number "4" after "OS":
Using current system time:
time = Sys.time()
str(time)
POSIXct[1:1], format: "2019-01-19 15:41:28.185"
newTime = strftime(Sys.time(), format="%H:%M:%OS4")
str(newTime)
chr "15:41:28.1851"
newTime
[1] "15:41:28.1851"
Thanks @JessicaBurnett, once I have the 'chr "15:41:28.1851"', how do you convert that back to a "time" that can be used in a conditional statement such as an "if" statement?
– talikarng
Jan 19 at 23:27
add a comment |
Using base::strftime(), you can obtain a character of the desired time to any divide of seconds you want. Below I have saved the current time to unit 1/1000 of a second, using the number "4" after "OS":
Using current system time:
time = Sys.time()
str(time)
POSIXct[1:1], format: "2019-01-19 15:41:28.185"
newTime = strftime(Sys.time(), format="%H:%M:%OS4")
str(newTime)
chr "15:41:28.1851"
newTime
[1] "15:41:28.1851"
Thanks @JessicaBurnett, once I have the 'chr "15:41:28.1851"', how do you convert that back to a "time" that can be used in a conditional statement such as an "if" statement?
– talikarng
Jan 19 at 23:27
add a comment |
Using base::strftime(), you can obtain a character of the desired time to any divide of seconds you want. Below I have saved the current time to unit 1/1000 of a second, using the number "4" after "OS":
Using current system time:
time = Sys.time()
str(time)
POSIXct[1:1], format: "2019-01-19 15:41:28.185"
newTime = strftime(Sys.time(), format="%H:%M:%OS4")
str(newTime)
chr "15:41:28.1851"
newTime
[1] "15:41:28.1851"
Using base::strftime(), you can obtain a character of the desired time to any divide of seconds you want. Below I have saved the current time to unit 1/1000 of a second, using the number "4" after "OS":
Using current system time:
time = Sys.time()
str(time)
POSIXct[1:1], format: "2019-01-19 15:41:28.185"
newTime = strftime(Sys.time(), format="%H:%M:%OS4")
str(newTime)
chr "15:41:28.1851"
newTime
[1] "15:41:28.1851"
edited Jan 19 at 21:50
answered Jan 19 at 21:37
Jessica BurnettJessica Burnett
115
115
Thanks @JessicaBurnett, once I have the 'chr "15:41:28.1851"', how do you convert that back to a "time" that can be used in a conditional statement such as an "if" statement?
– talikarng
Jan 19 at 23:27
add a comment |
Thanks @JessicaBurnett, once I have the 'chr "15:41:28.1851"', how do you convert that back to a "time" that can be used in a conditional statement such as an "if" statement?
– talikarng
Jan 19 at 23:27
Thanks @JessicaBurnett, once I have the 'chr "15:41:28.1851"', how do you convert that back to a "time" that can be used in a conditional statement such as an "if" statement?
– talikarng
Jan 19 at 23:27
Thanks @JessicaBurnett, once I have the 'chr "15:41:28.1851"', how do you convert that back to a "time" that can be used in a conditional statement such as an "if" statement?
– talikarng
Jan 19 at 23:27
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%2f54271311%2fhow-do-i-convert-characters-to-a-time-that-has-fractional-seconds-without-adding%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
lubridate package in R is your best bet!
– Data Science
Jan 19 at 21:18