0

I'm pretty new to r, and so was wondering if someone might be able to help with the error message I've been receiving.

I have a data.frame AUC_sheet which contains the column AUC_sheet$sys_timewhich is in POSIXct and represents the times that blood pressure readings were taken during an operation.

I would like to convert AUC_sheet out of POSIXct, so that I can get an accurate result during subsequent area under the curve calculations. I've used the following for loop to perform the conversion:

for(i in 1:length(AUC_sheet$sys_time)){
AUC_sheet$sys_time[i] <- as.numeric(difftime(time1 = AUC_sheet$sys_time[1],
                                             time2 = AUC_sheet$sys_time[i], units = "hours"))
}

But I keep getting an error message as follows

Error in as.POSIXct.numeric(value) : 'origin' must be supplied

I've tried using origin = "1970-01-01" but it tells me this is an unused argument.

Is there something glaringly obvious that I'm doing wrong?

Thanks in advance, and sorry if I've not provided enough data, I can post more as an edit if needed

EDIT AUC_sheet$sys_time looks like this

     sys_value            sys_time
       <dbl>              <time>
1         85 2013-08-28 12:48:24
2         NA 2013-08-28 12:48:39
3         NA 2013-08-28 12:48:54
4         NA 2013-08-28 12:49:08
5         NA 2013-08-28 12:49:24
6        170 2013-08-28 12:49:38
7        150 2013-08-28 12:49:54
8        167 2013-08-28 12:50:09
9        175 2013-08-28 12:50:24
10       167 2013-08-28 12:50:39
# ... with 549 more rows
MrFlick
  • 195,160
  • 17
  • 277
  • 295
cmo1
  • 3
  • 3

1 Answers1

1

Your problem is not the as.numeric call itself. The problem is that you are trying to write the result of that call to a column which is a POSIXct column. So, R tries to convert it to the correct format for you, and fails, because the conversion method requires an origin. If you write to a new column (or, better yet, write the for loop as a single vectorised operation to avoid the issue) then you shouldn't have a problem.

# make up some dummy data for testing
AUC = data.frame(sys_value = 1:100, sys_time = as.POSIXct(Sys.time() + 1:100))

for(i in 1:length(AUC$sys_time)){
  AUC$sys_time[i] <- as.numeric(difftime(time1 = AUC$sys_time[1],
                                     time2 = AUC$sys_time[i], units = "hours"))
} # this doesn't work, because you're mixing data types in the sys_time column


for(i in 1:length(AUC$sys_time)){
  AUC$sys_time_diff[i] <- as.numeric(difftime(time1 = AUC$sys_time[1],
                                          time2 = AUC$sys_time[i], units = "hours"))
} # this works, because the numeric data isn't being added to a time column
CPhil
  • 917
  • 5
  • 11