[英]How do I combine year month date time into a single datetime column using DBI in R?
Here's an example of my source data csv, df:这是我的源数据 csv、df 的示例:
Year : int 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 ...
Month : int 1 1 1 1 1 1 1 1 1 1 ...
DayofMonth : int 28 29 30 31 2 3 4 5 6 7 ...
Time : int 1605 1605 1610 1605 1900 1900 1900 1900 1900 1900
How can I combine the 4 columns into a single column into a datetime column in my database using SQLite and not dplyr?如何使用 SQLite 而不是 dplyr 将 4 列合并到数据库中的日期时间列中?
I hope the output can be something like: 2005-01-29 09:30:00 so that I can plot graphs.我希望输出可以类似于: 2005-01-29 09:30:00 以便我可以绘制图表。
You could use lubridate::make_datetime
:您可以使用lubridate::make_datetime
:
Year =c(2005, 2005, 2005)
Month = c(1,2,3)
DayofMonth = c( 28, 28, 30)
Time = c(1605, 1605, 1610)
lubridate::make_datetime(Year,Month,DayofMonth,floor(Time/100),Time%%100)
[1] "2005-01-28 16:05:00 UTC" "2005-02-28 16:05:00 UTC" "2005-03-30 16:10:00 UTC"
Base R, using Waldi's sample data (thanks!): Base R,使用 Waldi 的样本数据(谢谢!):
with(df, as.POSIXct(sprintf("%i-%02i-%i %02i:%02i:00",
Year, Month, DayofMonth, Time %/% 100, Time %% 100))
)
# [1] "2005-01-28 16:05:00 EST" "2005-02-28 16:05:00 EST" "2005-03-30 16:10:00 EST"
This is (possibly naïvely) assuming that you never have seconds or fractional minutes or Time
values that are not "meaningful" (ie, more than 59 minutes, more than 23 hours).这是(可能是天真的)假设您从来没有没有“有意义”的秒或小数分钟或Time
值(即超过 59 分钟,超过 23 小时)。
Data数据
df <- structure(list(Year = c(2005, 2005, 2005), Month = c(1, 2, 3), DayofMonth = c(28, 28, 30), Time = c(1605, 1605, 1610)), class = "data.frame", row.names = c(NA, -3L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.