How to convert character into time duration?












0















First of all, happy new year everyone.



I'm studying a way to calculate time spend in different type of activities collect by excel spreadsheet, but I'm facing issues to use time duration data.



After read the file all values of time come as character type and I'm unable to transforme into HH:MM:SS.



Dataframe example:



df <- data.frame(id=c(1,2,3,4,5,6),
name=c('Sean','Bob','Dylan',"Barbara","Louis","Marine"),
Swimming=c("00:00:00","00:30:22","00:42:22",
"00:50:53","00:20:11","00:30:12"),
Skating=c("00:10:23","00:10:22","00:02:22",
"00:20:53","00:30:11","00:10:12"))


I need to transform this CHR values of Swimming and Skating column into a time duration to manipulate them, I want to know for example, how many hours all of them spend doing swimming activities.



Find below what I've already tried:



Lubridate package (parse_date_time) function:



parse_date_time(df[3:4],"HMS")


Gives me this warning:




Warning message:
All formats failed to parse. No formats found.




Could you guys have any suggestion to transform this data in a way in which I'll can manipulate?



Thanks in advance!!



Happy 2019










share|improve this question

























  • I think this is a slightly misleading error as running parse_date_time(df$Swimming,"HMS") does work. That said, I'd recommend hms(df$Swimming) instead since you don't want these times in UTC

    – Emily Kothe
    Jan 1 at 23:14













  • Or in base R functionality, you can do - as.difftime(as.character(df$Swimming)) which by default looks for %H:%M:%S format times. Parsing into a complete date/time object won't allow you to sum the values - I think the lubridate equivalent is a "duration" - see resources.rstudio.com/the-essentials-of-data-science/…

    – thelatemail
    Jan 1 at 23:15








  • 1





    @EmilyKothe - as.duration(hms(df$Swimming)) I think is preferable. sum(hms(df$Swimming)) gives a really odd result while sum(as.duration(hms(df$Swimming))) gives a more expected result.

    – thelatemail
    Jan 1 at 23:25











  • @thelatemail when I try to do that sum(hms(df$Swimming)) I get this error message: "Error: All arguments must be numeric or NA"

    – tmangueira
    Jan 2 at 0:27













  • @tmangueira - using your exact df example as per the post and the code above? It all works for me.

    – thelatemail
    Jan 2 at 0:29
















0















First of all, happy new year everyone.



I'm studying a way to calculate time spend in different type of activities collect by excel spreadsheet, but I'm facing issues to use time duration data.



After read the file all values of time come as character type and I'm unable to transforme into HH:MM:SS.



Dataframe example:



df <- data.frame(id=c(1,2,3,4,5,6),
name=c('Sean','Bob','Dylan',"Barbara","Louis","Marine"),
Swimming=c("00:00:00","00:30:22","00:42:22",
"00:50:53","00:20:11","00:30:12"),
Skating=c("00:10:23","00:10:22","00:02:22",
"00:20:53","00:30:11","00:10:12"))


I need to transform this CHR values of Swimming and Skating column into a time duration to manipulate them, I want to know for example, how many hours all of them spend doing swimming activities.



Find below what I've already tried:



Lubridate package (parse_date_time) function:



parse_date_time(df[3:4],"HMS")


Gives me this warning:




Warning message:
All formats failed to parse. No formats found.




Could you guys have any suggestion to transform this data in a way in which I'll can manipulate?



Thanks in advance!!



Happy 2019










share|improve this question

























  • I think this is a slightly misleading error as running parse_date_time(df$Swimming,"HMS") does work. That said, I'd recommend hms(df$Swimming) instead since you don't want these times in UTC

    – Emily Kothe
    Jan 1 at 23:14













  • Or in base R functionality, you can do - as.difftime(as.character(df$Swimming)) which by default looks for %H:%M:%S format times. Parsing into a complete date/time object won't allow you to sum the values - I think the lubridate equivalent is a "duration" - see resources.rstudio.com/the-essentials-of-data-science/…

    – thelatemail
    Jan 1 at 23:15








  • 1





    @EmilyKothe - as.duration(hms(df$Swimming)) I think is preferable. sum(hms(df$Swimming)) gives a really odd result while sum(as.duration(hms(df$Swimming))) gives a more expected result.

    – thelatemail
    Jan 1 at 23:25











  • @thelatemail when I try to do that sum(hms(df$Swimming)) I get this error message: "Error: All arguments must be numeric or NA"

    – tmangueira
    Jan 2 at 0:27













  • @tmangueira - using your exact df example as per the post and the code above? It all works for me.

    – thelatemail
    Jan 2 at 0:29














0












0








0








First of all, happy new year everyone.



I'm studying a way to calculate time spend in different type of activities collect by excel spreadsheet, but I'm facing issues to use time duration data.



After read the file all values of time come as character type and I'm unable to transforme into HH:MM:SS.



Dataframe example:



df <- data.frame(id=c(1,2,3,4,5,6),
name=c('Sean','Bob','Dylan',"Barbara","Louis","Marine"),
Swimming=c("00:00:00","00:30:22","00:42:22",
"00:50:53","00:20:11","00:30:12"),
Skating=c("00:10:23","00:10:22","00:02:22",
"00:20:53","00:30:11","00:10:12"))


I need to transform this CHR values of Swimming and Skating column into a time duration to manipulate them, I want to know for example, how many hours all of them spend doing swimming activities.



Find below what I've already tried:



Lubridate package (parse_date_time) function:



parse_date_time(df[3:4],"HMS")


Gives me this warning:




Warning message:
All formats failed to parse. No formats found.




Could you guys have any suggestion to transform this data in a way in which I'll can manipulate?



Thanks in advance!!



Happy 2019










share|improve this question
















First of all, happy new year everyone.



I'm studying a way to calculate time spend in different type of activities collect by excel spreadsheet, but I'm facing issues to use time duration data.



After read the file all values of time come as character type and I'm unable to transforme into HH:MM:SS.



Dataframe example:



df <- data.frame(id=c(1,2,3,4,5,6),
name=c('Sean','Bob','Dylan',"Barbara","Louis","Marine"),
Swimming=c("00:00:00","00:30:22","00:42:22",
"00:50:53","00:20:11","00:30:12"),
Skating=c("00:10:23","00:10:22","00:02:22",
"00:20:53","00:30:11","00:10:12"))


I need to transform this CHR values of Swimming and Skating column into a time duration to manipulate them, I want to know for example, how many hours all of them spend doing swimming activities.



Find below what I've already tried:



Lubridate package (parse_date_time) function:



parse_date_time(df[3:4],"HMS")


Gives me this warning:




Warning message:
All formats failed to parse. No formats found.




Could you guys have any suggestion to transform this data in a way in which I'll can manipulate?



Thanks in advance!!



Happy 2019







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 0:12









Julius Vainora

38.1k76685




38.1k76685










asked Jan 1 at 23:00









tmangueiratmangueira

12




12













  • I think this is a slightly misleading error as running parse_date_time(df$Swimming,"HMS") does work. That said, I'd recommend hms(df$Swimming) instead since you don't want these times in UTC

    – Emily Kothe
    Jan 1 at 23:14













  • Or in base R functionality, you can do - as.difftime(as.character(df$Swimming)) which by default looks for %H:%M:%S format times. Parsing into a complete date/time object won't allow you to sum the values - I think the lubridate equivalent is a "duration" - see resources.rstudio.com/the-essentials-of-data-science/…

    – thelatemail
    Jan 1 at 23:15








  • 1





    @EmilyKothe - as.duration(hms(df$Swimming)) I think is preferable. sum(hms(df$Swimming)) gives a really odd result while sum(as.duration(hms(df$Swimming))) gives a more expected result.

    – thelatemail
    Jan 1 at 23:25











  • @thelatemail when I try to do that sum(hms(df$Swimming)) I get this error message: "Error: All arguments must be numeric or NA"

    – tmangueira
    Jan 2 at 0:27













  • @tmangueira - using your exact df example as per the post and the code above? It all works for me.

    – thelatemail
    Jan 2 at 0:29



















  • I think this is a slightly misleading error as running parse_date_time(df$Swimming,"HMS") does work. That said, I'd recommend hms(df$Swimming) instead since you don't want these times in UTC

    – Emily Kothe
    Jan 1 at 23:14













  • Or in base R functionality, you can do - as.difftime(as.character(df$Swimming)) which by default looks for %H:%M:%S format times. Parsing into a complete date/time object won't allow you to sum the values - I think the lubridate equivalent is a "duration" - see resources.rstudio.com/the-essentials-of-data-science/…

    – thelatemail
    Jan 1 at 23:15








  • 1





    @EmilyKothe - as.duration(hms(df$Swimming)) I think is preferable. sum(hms(df$Swimming)) gives a really odd result while sum(as.duration(hms(df$Swimming))) gives a more expected result.

    – thelatemail
    Jan 1 at 23:25











  • @thelatemail when I try to do that sum(hms(df$Swimming)) I get this error message: "Error: All arguments must be numeric or NA"

    – tmangueira
    Jan 2 at 0:27













  • @tmangueira - using your exact df example as per the post and the code above? It all works for me.

    – thelatemail
    Jan 2 at 0:29

















I think this is a slightly misleading error as running parse_date_time(df$Swimming,"HMS") does work. That said, I'd recommend hms(df$Swimming) instead since you don't want these times in UTC

– Emily Kothe
Jan 1 at 23:14







I think this is a slightly misleading error as running parse_date_time(df$Swimming,"HMS") does work. That said, I'd recommend hms(df$Swimming) instead since you don't want these times in UTC

– Emily Kothe
Jan 1 at 23:14















Or in base R functionality, you can do - as.difftime(as.character(df$Swimming)) which by default looks for %H:%M:%S format times. Parsing into a complete date/time object won't allow you to sum the values - I think the lubridate equivalent is a "duration" - see resources.rstudio.com/the-essentials-of-data-science/…

– thelatemail
Jan 1 at 23:15







Or in base R functionality, you can do - as.difftime(as.character(df$Swimming)) which by default looks for %H:%M:%S format times. Parsing into a complete date/time object won't allow you to sum the values - I think the lubridate equivalent is a "duration" - see resources.rstudio.com/the-essentials-of-data-science/…

– thelatemail
Jan 1 at 23:15






1




1





@EmilyKothe - as.duration(hms(df$Swimming)) I think is preferable. sum(hms(df$Swimming)) gives a really odd result while sum(as.duration(hms(df$Swimming))) gives a more expected result.

– thelatemail
Jan 1 at 23:25





@EmilyKothe - as.duration(hms(df$Swimming)) I think is preferable. sum(hms(df$Swimming)) gives a really odd result while sum(as.duration(hms(df$Swimming))) gives a more expected result.

– thelatemail
Jan 1 at 23:25













@thelatemail when I try to do that sum(hms(df$Swimming)) I get this error message: "Error: All arguments must be numeric or NA"

– tmangueira
Jan 2 at 0:27







@thelatemail when I try to do that sum(hms(df$Swimming)) I get this error message: "Error: All arguments must be numeric or NA"

– tmangueira
Jan 2 at 0:27















@tmangueira - using your exact df example as per the post and the code above? It all works for me.

– thelatemail
Jan 2 at 0:29





@tmangueira - using your exact df example as per the post and the code above? It all works for me.

– thelatemail
Jan 2 at 0:29












0






active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999601%2fhow-to-convert-character-into-time-duration%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999601%2fhow-to-convert-character-into-time-duration%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement