Reputation: 1132
I am trying to compute date with 3600 to get what I want:
Here is the code I am writing:
Time_subset$Angio_time = Time_subset$Angio_time/3600
And get this error:
Error in Time_subset$Angio_time/3600 :
non-numeric argument to binary operator
Prior to the above, I run another code:
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]
)
Here is the sample test data:
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"))
Is there a way to solve this issue, when passing the bellow code?
Time_subset$Angio_time = Time_subset$Angio_time/3600
Upvotes: 0
Views: 225
Reputation: 160447
Your data has no timestamps, just strings. You need to convert them to POSIXt
with something like
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"
But getting past that, "time" has no concept of "division" operations. I suspect that you have munged your data by concatenating the strings of X4.18.Angio.date.time
and X3.01.Onset.of.Symptoms
, where instead you're looking for the difference of them.
From your /3600
, I suspect you want that in hours.
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
Better yet, fix the original columns and add the difference:
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
The " hours"
literal on Angio_time
values is cosmetic, it is internally a number that you can do math on. If you want to remove it, instead use as.numeric(difftime(..))
.
Upvotes: 1