从日期时间列R中删除日期

时间:2020-04-28 17:02:56

标签: r date lubridate posixct strptime

我的excel(.xlsx)数据集中有一列时间(名为“ necro_time”;格式为%H:%M:%S)。当我使用 read_excel 函数(库readxl)导入数据(“测试”)时,它会自动将我的时间列格式化为POSIXct,格式为Y-M-D H:M:S,实际上是使用随机日期“ 1899-12-31“到我的每一个时代。当我使用strptime函数尝试删除日期时,只保留返回NA的时间。我已经尝试了日期时间格式的所有组合,并创建了新列,并尝试以这种方式解析日期时间(无论如何我都不想这样做),并且没有任何效果。

建议?

我的数据框的结构:

> str(test)


Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1051 obs. of  43 variables:

 ...

 $ necro_date               : POSIXct, format: "1900-01-01" "2018-12-19" "2018-12-19" ...

 $ necro_time               : POSIXct, format: "1899-12-31 23:59:00" "1899-12-31 11:12:00" "1899-12-31 11:03:00" ...

我正在尝试运行的代码:

test$necro_time = (strptime(test$necro_time, format = "%H:%M:%S")

返回:

> str(test)

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1051 obs. of  43 variables:

...

 $ necro_date               : POSIXct, format: "1900-01-01" "2018-12-19" "2018-12-19" ...

 $ necro_time               : POSIXct, format: NA NA NA ...

我也尝试过:

test$necro_time = as.POSIXct(strptime(test$necro_time, format = "%Y-%M-%D"))

test$necro_time = as.POSIXct(strptime(test$necro_time, format = "%Y-%M-%D %H:%M:%S"))

test$necro_time = strptime(test$necro_time, format = "%I:%M %p")

all全部返回NA s的完整列。我觉得这很简单,但我不知道自己在做什么错。预先感谢。

0 个答案:

没有答案