为什么我会得到:Error in : non-numeric argument to binary operator with dates?
Why do I get: Error in : non-numeric argument to binary operator with dates?
我正在尝试用 3600 计算日期以获得我想要的结果:
这是我正在编写的代码:
Time_subset$Angio_time = Time_subset$Angio_time/3600
并得到这个错误:
Error in Time_subset$Angio_time/3600 :
non-numeric argument to binary operator
在上面之前,我运行另一个代码:
in_hosp = dt_test_first_20$X2.39.Admission.Method == '3. already in this hospital'
dt_test_first_20$Angio_time[in_hosp] = with(dt_test_first_20,
X4.18.Angio.date.time[in_hosp] - X3.01.Onset.of.Symptoms[in_hosp]
)
这里是样本测试数据:
structure(list(X4.18.Angio.date.time = c("18/02/2021 11:46",
"24/02/2021 15:57", "24/02/2021 17:12", "08/02/2021 15:30", "11/02/2021 15:28",
"20/04/2020 15:00", "26/01/2021 11:09", "04/03/2021 12:45", "23/04/2020 10:00",
"06/01/2021 16:00", "09/10/2020 12:00", "08/06/2020 09:30", "23/07/2020 11:49",
"07/01/2021 17:00", "17/09/2020 11:17", "03/12/2020 12:45", "08/01/2021 15:12",
"12/02/2021 12:42", "18/02/2021 15:02", "19/11/2020 13:11"),
X3.01.Onset.of.Symptoms = c("13/02/2021 12:45", "17/02/2021 13:46",
"15/02/2021 09:00", "31/01/2021 09:00", "31/01/2021 17:59",
"17/04/2020 22:30", "18/01/2021 15:00", "28/02/2021 23:29",
"21/04/2020 18:00", "01/01/2021 22:30", "09/10/2020 09:00",
"02/06/2020 19:00", "21/07/2020 08:45", "29/12/2020 10:00",
"15/09/2020 11:45", "23/11/2020 22:00", "01/01/2021 08:55",
"09/02/2021 09:30", "14/02/2021 17:10", "19/11/2020 01:00"
), X2.39.Admission.Method = c("1. direct admission via emergency service",
"3. already in this hospital", "1. direct admission via emergency service",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"1. direct admission via emergency service", "3. already in this hospital",
"3. already in this hospital", "1. direct admission via emergency service",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"1. direct admission via emergency service", "3. already in this hospital",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"2. self presenter to this hospital"), Angio_time = c(NA,
"24/02/2021 15:5717/02/2021 13:46", NA, NA, NA, NA, NA, NA,
NA, NA, "09/10/2020 12:0009/10/2020 09:00", "08/06/2020 09:3002/06/2020 19:00",
NA, NA, NA, NA, "08/01/2021 15:1201/01/2021 08:55", NA, NA,
NA)), row.names = c(NA, -20L), class = c("tbl_df", "tbl",
"data.frame"))
在传递下面的代码时,有没有办法解决这个问题?
Time_subset$Angio_time = Time_subset$Angio_time/3600
您的数据没有时间戳,只有字符串。您需要使用
之类的东西将它们转换为 POSIXt
as.POSIXct(Time_subset[[1]], format = "%d/%m/%Y %H:%M")
# [1] "2021-02-18 11:46:00 EST" "2021-02-24 15:57:00 EST" "2021-02-24 17:12:00 EST" "2021-02-08 15:30:00 EST"
# [5] "2021-02-11 15:28:00 EST" "2020-04-20 15:00:00 EDT" "2021-01-26 11:09:00 EST" "2021-03-04 12:45:00 EST"
# [9] "2020-04-23 10:00:00 EDT" "2021-01-06 16:00:00 EST" "2020-10-09 12:00:00 EDT" "2020-06-08 09:30:00 EDT"
# [13] "2020-07-23 11:49:00 EDT" "2021-01-07 17:00:00 EST" "2020-09-17 11:17:00 EDT" "2020-12-03 12:45:00 EST"
# [17] "2021-01-08 15:12:00 EST" "2021-02-12 12:42:00 EST" "2021-02-18 15:02:00 EST" "2020-11-19 13:11:00 EST"
不过话说回来,“时间”没有“除法”运算的概念。我怀疑您通过连接 X4.18.Angio.date.time
和 X3.01.Onset.of.Symptoms
的字符串来修改数据,而您正在寻找它们的 差异 。
从你的 /3600
来看,我怀疑你想在几个小时内完成。
difftime(
as.POSIXct(Time_subset[[1]], format = "%d/%m/%Y %H:%M"),
as.POSIXct(Time_subset[[2]], format = "%d/%m/%Y %H:%M"),
units = "hours")
# Time differences in hours
# [1] 119.02 170.18 224.20 198.50 261.48 64.50 188.15 85.27 40.00 113.50 3.00 134.50 51.07 223.00 47.53 230.75 174.28
# [18] 75.20 93.87 12.18
更好的是,修复原始列并添加差异:
Time_subset[1:2] <- lapply(Time_subset[1:2], as.POSIXct, format = "%d/%m/%Y %H:%M")
Time_subset$Angio_time <- difftime(Time_subset[[1]], Time_subset[[2]], units = "hours")
Time_subset
# # A tibble: 20 x 4
# X4.18.Angio.date.time X3.01.Onset.of.Symptoms X2.39.Admission.Method Angio_time
# <dttm> <dttm> <chr> <drtn>
# 1 2021-02-18 11:46:00 2021-02-13 12:45:00 1. direct admission via emergency service 119.02 hours
# 2 2021-02-24 15:57:00 2021-02-17 13:46:00 3. already in this hospital 170.18 hours
# 3 2021-02-24 17:12:00 2021-02-15 09:00:00 1. direct admission via emergency service 224.20 hours
# 4 2021-02-08 15:30:00 2021-01-31 09:00:00 1. direct admission via emergency service 198.50 hours
# 5 2021-02-11 15:28:00 2021-01-31 17:59:00 1. direct admission via emergency service 261.48 hours
# 6 2020-04-20 15:00:00 2020-04-17 22:30:00 1. direct admission via emergency service 64.50 hours
# 7 2021-01-26 11:09:00 2021-01-18 15:00:00 1. direct admission via emergency service 188.15 hours
# 8 2021-03-04 12:45:00 2021-02-28 23:29:00 1. direct admission via emergency service 85.27 hours
# 9 2020-04-23 10:00:00 2020-04-21 18:00:00 1. direct admission via emergency service 40.00 hours
# 10 2021-01-06 16:00:00 2021-01-01 22:30:00 1. direct admission via emergency service 113.50 hours
# 11 2020-10-09 12:00:00 2020-10-09 09:00:00 3. already in this hospital 3.00 hours
# 12 2020-06-08 09:30:00 2020-06-02 19:00:00 3. already in this hospital 134.50 hours
# 13 2020-07-23 11:49:00 2020-07-21 08:45:00 1. direct admission via emergency service 51.07 hours
# 14 2021-01-07 17:00:00 2020-12-29 10:00:00 1. direct admission via emergency service 223.00 hours
# 15 2020-09-17 11:17:00 2020-09-15 11:45:00 1. direct admission via emergency service 47.53 hours
# 16 2020-12-03 12:45:00 2020-11-23 22:00:00 1. direct admission via emergency service 230.75 hours
# 17 2021-01-08 15:12:00 2021-01-01 08:55:00 3. already in this hospital 174.28 hours
# 18 2021-02-12 12:42:00 2021-02-09 09:30:00 1. direct admission via emergency service 75.20 hours
# 19 2021-02-18 15:02:00 2021-02-14 17:10:00 1. direct admission via emergency service 93.87 hours
# 20 2020-11-19 13:11:00 2020-11-19 01:00:00 2. self presenter to this hospital 12.18 hours
Angio_time
值上的 " hours"
文字是装饰性的,它在内部是一个数字,您可以对其进行数学运算。如果要删除它,请改用 as.numeric(difftime(..))
.
我正在尝试用 3600 计算日期以获得我想要的结果:
这是我正在编写的代码:
Time_subset$Angio_time = Time_subset$Angio_time/3600
并得到这个错误:
Error in Time_subset$Angio_time/3600 :
non-numeric argument to binary operator
在上面之前,我运行另一个代码:
in_hosp = dt_test_first_20$X2.39.Admission.Method == '3. already in this hospital'
dt_test_first_20$Angio_time[in_hosp] = with(dt_test_first_20,
X4.18.Angio.date.time[in_hosp] - X3.01.Onset.of.Symptoms[in_hosp]
)
这里是样本测试数据:
structure(list(X4.18.Angio.date.time = c("18/02/2021 11:46",
"24/02/2021 15:57", "24/02/2021 17:12", "08/02/2021 15:30", "11/02/2021 15:28",
"20/04/2020 15:00", "26/01/2021 11:09", "04/03/2021 12:45", "23/04/2020 10:00",
"06/01/2021 16:00", "09/10/2020 12:00", "08/06/2020 09:30", "23/07/2020 11:49",
"07/01/2021 17:00", "17/09/2020 11:17", "03/12/2020 12:45", "08/01/2021 15:12",
"12/02/2021 12:42", "18/02/2021 15:02", "19/11/2020 13:11"),
X3.01.Onset.of.Symptoms = c("13/02/2021 12:45", "17/02/2021 13:46",
"15/02/2021 09:00", "31/01/2021 09:00", "31/01/2021 17:59",
"17/04/2020 22:30", "18/01/2021 15:00", "28/02/2021 23:29",
"21/04/2020 18:00", "01/01/2021 22:30", "09/10/2020 09:00",
"02/06/2020 19:00", "21/07/2020 08:45", "29/12/2020 10:00",
"15/09/2020 11:45", "23/11/2020 22:00", "01/01/2021 08:55",
"09/02/2021 09:30", "14/02/2021 17:10", "19/11/2020 01:00"
), X2.39.Admission.Method = c("1. direct admission via emergency service",
"3. already in this hospital", "1. direct admission via emergency service",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"1. direct admission via emergency service", "3. already in this hospital",
"3. already in this hospital", "1. direct admission via emergency service",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"1. direct admission via emergency service", "3. already in this hospital",
"1. direct admission via emergency service", "1. direct admission via emergency service",
"2. self presenter to this hospital"), Angio_time = c(NA,
"24/02/2021 15:5717/02/2021 13:46", NA, NA, NA, NA, NA, NA,
NA, NA, "09/10/2020 12:0009/10/2020 09:00", "08/06/2020 09:3002/06/2020 19:00",
NA, NA, NA, NA, "08/01/2021 15:1201/01/2021 08:55", NA, NA,
NA)), row.names = c(NA, -20L), class = c("tbl_df", "tbl",
"data.frame"))
在传递下面的代码时,有没有办法解决这个问题?
Time_subset$Angio_time = Time_subset$Angio_time/3600
您的数据没有时间戳,只有字符串。您需要使用
之类的东西将它们转换为POSIXt
as.POSIXct(Time_subset[[1]], format = "%d/%m/%Y %H:%M")
# [1] "2021-02-18 11:46:00 EST" "2021-02-24 15:57:00 EST" "2021-02-24 17:12:00 EST" "2021-02-08 15:30:00 EST"
# [5] "2021-02-11 15:28:00 EST" "2020-04-20 15:00:00 EDT" "2021-01-26 11:09:00 EST" "2021-03-04 12:45:00 EST"
# [9] "2020-04-23 10:00:00 EDT" "2021-01-06 16:00:00 EST" "2020-10-09 12:00:00 EDT" "2020-06-08 09:30:00 EDT"
# [13] "2020-07-23 11:49:00 EDT" "2021-01-07 17:00:00 EST" "2020-09-17 11:17:00 EDT" "2020-12-03 12:45:00 EST"
# [17] "2021-01-08 15:12:00 EST" "2021-02-12 12:42:00 EST" "2021-02-18 15:02:00 EST" "2020-11-19 13:11:00 EST"
不过话说回来,“时间”没有“除法”运算的概念。我怀疑您通过连接 X4.18.Angio.date.time
和 X3.01.Onset.of.Symptoms
的字符串来修改数据,而您正在寻找它们的 差异 。
从你的 /3600
来看,我怀疑你想在几个小时内完成。
difftime(
as.POSIXct(Time_subset[[1]], format = "%d/%m/%Y %H:%M"),
as.POSIXct(Time_subset[[2]], format = "%d/%m/%Y %H:%M"),
units = "hours")
# Time differences in hours
# [1] 119.02 170.18 224.20 198.50 261.48 64.50 188.15 85.27 40.00 113.50 3.00 134.50 51.07 223.00 47.53 230.75 174.28
# [18] 75.20 93.87 12.18
更好的是,修复原始列并添加差异:
Time_subset[1:2] <- lapply(Time_subset[1:2], as.POSIXct, format = "%d/%m/%Y %H:%M")
Time_subset$Angio_time <- difftime(Time_subset[[1]], Time_subset[[2]], units = "hours")
Time_subset
# # A tibble: 20 x 4
# X4.18.Angio.date.time X3.01.Onset.of.Symptoms X2.39.Admission.Method Angio_time
# <dttm> <dttm> <chr> <drtn>
# 1 2021-02-18 11:46:00 2021-02-13 12:45:00 1. direct admission via emergency service 119.02 hours
# 2 2021-02-24 15:57:00 2021-02-17 13:46:00 3. already in this hospital 170.18 hours
# 3 2021-02-24 17:12:00 2021-02-15 09:00:00 1. direct admission via emergency service 224.20 hours
# 4 2021-02-08 15:30:00 2021-01-31 09:00:00 1. direct admission via emergency service 198.50 hours
# 5 2021-02-11 15:28:00 2021-01-31 17:59:00 1. direct admission via emergency service 261.48 hours
# 6 2020-04-20 15:00:00 2020-04-17 22:30:00 1. direct admission via emergency service 64.50 hours
# 7 2021-01-26 11:09:00 2021-01-18 15:00:00 1. direct admission via emergency service 188.15 hours
# 8 2021-03-04 12:45:00 2021-02-28 23:29:00 1. direct admission via emergency service 85.27 hours
# 9 2020-04-23 10:00:00 2020-04-21 18:00:00 1. direct admission via emergency service 40.00 hours
# 10 2021-01-06 16:00:00 2021-01-01 22:30:00 1. direct admission via emergency service 113.50 hours
# 11 2020-10-09 12:00:00 2020-10-09 09:00:00 3. already in this hospital 3.00 hours
# 12 2020-06-08 09:30:00 2020-06-02 19:00:00 3. already in this hospital 134.50 hours
# 13 2020-07-23 11:49:00 2020-07-21 08:45:00 1. direct admission via emergency service 51.07 hours
# 14 2021-01-07 17:00:00 2020-12-29 10:00:00 1. direct admission via emergency service 223.00 hours
# 15 2020-09-17 11:17:00 2020-09-15 11:45:00 1. direct admission via emergency service 47.53 hours
# 16 2020-12-03 12:45:00 2020-11-23 22:00:00 1. direct admission via emergency service 230.75 hours
# 17 2021-01-08 15:12:00 2021-01-01 08:55:00 3. already in this hospital 174.28 hours
# 18 2021-02-12 12:42:00 2021-02-09 09:30:00 1. direct admission via emergency service 75.20 hours
# 19 2021-02-18 15:02:00 2021-02-14 17:10:00 1. direct admission via emergency service 93.87 hours
# 20 2020-11-19 13:11:00 2020-11-19 01:00:00 2. self presenter to this hospital 12.18 hours
Angio_time
值上的 " hours"
文字是装饰性的,它在内部是一个数字,您可以对其进行数学运算。如果要删除它,请改用 as.numeric(difftime(..))
.