您如何将矢量从r中更改为r中的int?

发布于 2025-02-03 14:18:11 字数 721 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女的英雄梦 2025-02-10 14:18:11

正如用户@yonyambu指出的那样,他们回答了这个问题在这里

也可以使用as.itime来概括

as.numeric(data.table::as.ITime(Q1$Start_Time))

45000 51900 35100 64200

但 对于HH:MM:SS格式,我使用了突变,

Q1 <- Q1 %>% mutate(Start_Time = hms::hms(Start_Time))

感谢所有参与其中的人!

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

as.numeric(data.table::as.ITime(Q1$Start_Time))

and returned the values for

45000 51900 35100 64200

For anyone curious about converting it back to a HH:MM:SS format I used mutate

Q1 <- Q1 %>% mutate(Start_Time = hms::hms(Start_Time))

Thanks to everyone who pitched in!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文