【问题标题】:how to convert character type time to int?如何将字符类型时间转换为int?
【发布时间】:2017-06-07 11:21:03
【问题描述】:

我有一个包含时间的数据框,以下列方式作为字符类型。

time=c("5 mins 30 seconds","1 min 46 seconds","6 mins 40 seconds","20 seconds","2 seconds")
student=c("A","B","C")
df<-data.frame(student,time)

我想对他们花费的时间进行排序,然后进行绘图。我应该如何进行?
还有如何将时间转换为以秒为单位的 INT 类型?

【问题讨论】:

  • 格式是否一致,如果是的话我们可以通过解析字符串来实现。

标签: r


【解决方案1】:

如果格式保持不变,我们可以在R中使用strptime函数

time<- c("5 mins 30 seconds","3 mins 46 seconds","6 mins 40 seconds")
times <- strptime(time, "%M mins %S seconds")
times <- format(times, "%H:%M:%S")
student=c("A","B","C")
df<-data.frame(student,times)

【讨论】:

  • 如果给出的字段是上面给出的 - 混合。那怎么处理呢?
  • 你必须解析时间戳。
  • R 中的 parse_date_time 可能有用。
  • 你能详细解释一下吗?
猜你喜欢
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
  • 2020-12-13
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多