简体   繁体   English

将数据帧转换为r中的月/日/年hr:min:sec(即01/15/19 00:06:00)的chron日期时间object的动物园时间序列构造?

[英]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))
I want to convert above data into time-series format(month/day/year hr:min:sec, ie 01/15/19 00:06:00) and output should look like: 我想将上述数据转换为时间序列格式(月/日/年 hr:min:sec,即 01/15/19 00:06:00)和 output 应该如下所示:

 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_hmsDateTime列转换为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.

相关问题 如何在 R 中将 mutate() 与 Date_Time 月/日/年 00:00 一起使用 - How to use mutate() with a Date_Time month/Day/Year 00:00 in R How to use 'read.production' function in R “StreamMetabolism” package to get dates in the format mm/dd/yy hh:mm:ss (eg,01/15/19 00:06:00) - How to use 'read.production' function in R “StreamMetabolism” package to get dates in the format mm/dd/yy hh:mm:ss (e.g.,01/15/19 00:06:00) 将几个数据框的单独列(年,日,年,时,分,秒)转换为R中的日期对象/时间戳 - Convert several data frame separate columns (year, dayOfYear, Hour, Min, Sec) into a date object/timestamp in R 我的数据集中有一列 eventDate (2000-01-01T00:06:00)。 我如何将它们分成日期和时间? - I have a column in my dataset, eventDate (2000-01-01T00:06:00). How do I split them into date and time? R:将年月数据框转换为时间序列 - R: Transforming a year-month data frame into a time series 如何在Zoo中拆分时间日期和时间对象以进行聚合 - How to split a chron date & time object in zoo for aggregation 将日期转换为时间序列的月/年格式 - Convert date to month/year format for time series 按时钟时间聚合带时间戳的动物园对象(即不仅仅是动物园对象中的时间) - Aggregating a timestamped zoo object by clock time (i.e. not solely by time in the zoo object) 按月,日,年,然后按AM / PM的时间进行R排序时间,并将时间转换为24小时格式 - R-sort time by Month,day,Year and then time which is in AM/PM and also convert the time to 24hr format R中是否有可以将“时间”因子(00:00:00)转换为秒的函数 - Is there a function in R that can convert a "time" factor (00:00:00) into seconds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM