您如何将矢量从r中更改为r中的int?
r的新用户在这里宽恕了我的无知,但我遇到了一些束缚!
我目前正在处理一个名为 Q1 的数据集,该数据集具有显示 start_time 的列,该列显示了HH:MM,但其向量为ChR,而不是INT或NUM。
我目前正在尝试将其转换为int或num,以便我可以将其格式更改为HH:MM:SS,但很难做到这一点。
例如:
Date <- c("11/22/22", "11/23/22", "11/24/22", "11/25/22")
Start_Time <- c("12:30", "14:25", "09:45", "17:50")
Q1 <- data.frame(Date, Start_Time)
到目前为止,我已经尝试过,
strptime(Q1$Start_Time, format = "%H:%M:%S")
as.integer(Q1$Start_Time)
Q1 <- transform(Q1, Start_Time = as.integer(Start_Time))
Q1 <- transform(Q1, Start_Time = as.numeric(Start_Time))
但是所有这些都通过整列返回了NA的值,或者给了我关于CRECION引入的NAS的错误。
有人有想法吗?任何帮助将不胜感激!谢谢你!
New user of R here so forgive my ignorance but I've hit a bit of a bind!
I am currently working on a data set called Q1 which has a column for Start_Time that is displaying HH:MM except its vector is CHR and not INT or NUM.
I am currently trying to convert it to an INT or NUM so that I may change its format to HH:MM:SS but am having trouble doing so.
For Example:
Date <- c("11/22/22", "11/23/22", "11/24/22", "11/25/22")
Start_Time <- c("12:30", "14:25", "09:45", "17:50")
Q1 <- data.frame(Date, Start_Time)
So far I have tried
strptime(Q1$Start_Time, format = "%H:%M:%S")
as.integer(Q1$Start_Time)
Q1 <- transform(Q1, Start_Time = as.integer(Start_Time))
Q1 <- transform(Q1, Start_Time = as.numeric(Start_Time))
But all of these have returned a value of NA through my whole column or given me an error about NAs introduced by coercion.
Does anyone have any ideas? Any help would be greatly appreciated! Thank You!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如用户@yonyambu指出的那样,他们回答了这个问题在这里,
也可以使用as.itime来概括
它
但 对于HH:MM:SS格式,我使用了突变,
感谢所有参与其中的人!
as user @onyambu pointed out this question was answered by them here
But to also sum it up using as.ITime from the data.table worked perfectly
and returned the values for
For anyone curious about converting it back to a HH:MM:SS format I used mutate
Thanks to everyone who pitched in!