[英]Convert data frame into zoo time series constructe with a chron date time object of month/day/year hr:min:sec (i.e. 01/15/19 00:06:00) in r?
Hi I am trying to use the "StreamMetabolism' package in R to calculating stream metabolism ( https://cran.rproject.org/web/packages/StreamMetabolism/StreamMetabolism.pdf ). In order to run the codes, dates need to be in a specific format that I am struggling to create. Hi I am trying to use the "StreamMetabolism' package in R to calculating stream metabolism ( https://cran.rproject.org/web/packages/StreamMetabolism/StreamMetabolism.pdf ). In order to run the codes, dates need to be以我正在努力创建的特定格式。
I have a csv file in the following format:我有一个 csv 文件,格式如下:
structure(list(DateTime = structure(1:18, .Label = c("(01/15/2019 00:06:00)", "(01/15/2019 00:21:00)", "(01/15/2019 00:36:00)", "(01/15/2019 00:51:00)", "(01/15/2019 01:06:00)", "(01/15/2019 01:21:00)", "(01/15/2019 01:36:00)", "(01/15/2019 01:51:00)", "(01/15/2019 02:06:00)", "(01/15/2019 02:21:00)", "(01/15/2019 02:36:00)", "(01/15/2019 02:51:00)", "(01/15/2019 03:06:00)", "(01/15/2019 03:21:00)", "(01/15/2019 03:36:00)", "(01/15/2019 03:51:00)", "(01/15/2019 04:06:00)", "(01/15/2019 04:21:00)"), class = "factor"), Temp = c(16.947, 16.862, 16.752, 16.735, 16.65, 16.608, 16.523, 16.455, 16.412, 16.361, 16.293, 16.25, 16.267, 16.216, 16.148, 16.114, 16.054, 16.046), DO = c(8.45, 8.429, 8.425, 8.379, 8.38, 8.358, 8.354, 8.344, 8.334, 8.323, 8.329, 8.314, 8.291, 8.29, 8.298, 8.29, 8.296, 8.289)), .Names = c("DateTime", "Temp", "DO"), class = "data.frame", row.names = c(NA, -18L))
Temp DO (01/15/2019 00:06:00) 16.947 8.45 (01/15/2019 00:21:00) 16.862 8.429 (01/15/2019 00:36:00) 16.752 8.425 (01/15/2019 00:51:00) 16.735 8.379 (01/15/2019 01:06:00) 16.65 8.38 (01/15/2019 01:21:00) 16.608 8.358 (01/15/2019 01:36:00) 16.523 8.354 (01/15/2019 01:51:00) 16.455 8.344 (01/15/2019 02:06:00) 16.412 8.334
Can anyone help with this?有人能帮忙吗?
If your goal is to modify the DateTime
column without creating a new one, you could try this:如果您的目标是修改DateTime
列而不创建新列,则可以尝试以下操作:
df$DateTime <- strptime(df$DateTime, "%m/%d/%Y %H:%M:%S")
(where df
is the name of the data frame you are using) (其中df
是您正在使用的数据框的名称)
The strptime
function gives all the dates the format %m/%d/%Y %H:%M:%S
. strptime
function 为所有日期提供格式%m/%d/%Y %H:%M:%S
。
You can use mdy_hms
to convert DateTime
column to POSIXct
class您可以使用mdy_hms
将DateTime
列转换为POSIXct
class
library(dplyr)
library(lubridate)
df <- df %>% mutate(DateTime = mdy_hms(DateTime))
df
# DateTime Temp DO
#1 2019-01-15 00:06:00 16.947 8.450
#2 2019-01-15 00:21:00 16.862 8.429
#3 2019-01-15 00:36:00 16.752 8.425
#4 2019-01-15 00:51:00 16.735 8.379
#5 2019-01-15 01:06:00 16.650 8.380
#6 2019-01-15 01:21:00 16.608 8.358
#7 2019-01-15 01:36:00 16.523 8.354
#8 2019-01-15 01:51:00 16.455 8.344
#9 2019-01-15 02:06:00 16.412 8.334
#10 2019-01-15 02:21:00 16.361 8.323
#11 2019-01-15 02:36:00 16.293 8.329
#12 2019-01-15 02:51:00 16.250 8.314
#13 2019-01-15 03:06:00 16.267 8.291
#14 2019-01-15 03:21:00 16.216 8.290
#15 2019-01-15 03:36:00 16.148 8.298
#16 2019-01-15 03:51:00 16.114 8.290
#17 2019-01-15 04:06:00 16.054 8.296
#18 2019-01-15 04:21:00 16.046 8.289
Using base R, you can do the same by:使用基础 R,您可以通过以下方式执行相同操作:
df$DateTime <- as.POSIXct(df$DateTime, format = '(%m/%d/%Y %T)', tz = 'UTC')
You can convert it to time series by doing:您可以通过执行以下操作将其转换为时间序列:
xts::xts(df[-1], order.by = df[[1]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.