-1

I want to substract timeA with timearriving and timeL with timeleaving but I get this error:

"Error in unclass(e1) - e2 : non-numeric argument to binary operator"

When you see that error message, it means that you're trying to perform a binary operation with something that isn't a number. I understand the error but I wanted to ask is there is a way I can perform these calculations? I provided a sample image of my dataset

    number  id    location timearriving timeleaving       timeA               timeL         person   late
1  214980 900264  1001.18         NULL        NULL 2016-09-15 10:00:00 2016-09-15 12:00:00 Teacher
2  215708 900264  1001.18     07:55:06    09:59:58 2016-09-22 10:00:00 2016-09-22 12:00:00 Teacher
3  216388 900264  1001.18     08:00:22    09:54:06 2016-09-29 10:00:00 2016-09-29 12:00:00 Teacher
4  217106 900264  1001.18     08:40:15    09:53:07 2016-10-05 10:00:00 2016-10-05 12:00:00 Teacher
5  217250 900264  1001.18     08:03:47    09:52:59 2016-10-06 10:00:00 2016-10-06 12:00:00 Teacher
6  217808 900264  1001.18         NULL        NULL 2016-10-12 10:00:00 2016-10-12 12:00:00 Teacher
7  217952 900264  1001.18     08:01:44    09:51:45 2016-10-13 10:00:00 2016-10-13 12:00:00 Teacher
8  218640 900264  1001.18     08:04:04    09:57:24 2016-10-19 10:00:00 2016-10-19 12:00:00 Teacher
9  218788 900264  1001.18     07:59:52    09:50:17 2016-10-20 10:00:00 2016-10-20 12:00:00 Teacher
10 219397 900264  1001.18     08:01:06    09:51:05 2016-10-26 10:00:00 2016-10-26 12:00:00 Teacher
11 219541 900264  1001.18     08:05:29    09:56:04 2016-10-27 10:00:00 2016-10-27 12:00:00 Teacher
12 220273 900264  1001.18     08:09:20    09:57:46 2016-11-02 09:00:00 2016-11-02 11:00:00 Teacher
13 220419 900264  1001.18     08:09:05    09:59:53 2016-11-03 09:00:00 2016-11-03 11:00:00 Teacher

Here I added a new column with the name "late". I want to subtract TimeA- timearriving I did this using this code:

dataset["late"] <- NA
dataset$late <- dataset$timeA - dataset$timearriving

then the error was:

Error in unclass(e1) - e2 : non-numeric argument to binary operator

Now I tried to convert them like you said: timeA <- ymd_hms(timeA )

timearriving <- hms(timearriving ) Warning message: In .parse_hms(..., order = "HMS", quiet = quiet) : Some strings failed to parse

lmo
  • 37,904
  • 9
  • 56
  • 69
gtcodes
  • 15
  • 2
  • 5

1 Answers1

3

Since you don't provide a reproducible example I will illustrate using one value for each variable e.g.:

library(lubridate)
timeleaving <- hms("09:59:33")
timeA <- ymd_hms("2017-02-16 10:00:00")

You could use:

timeleaving <- ymd_hms(paste(floor_date(timeA, "days"), timeleaving))
dif <- timeA -timeleaving  

Time difference of 27 secs

Edited since the data was added to the original question:

data$timeleaving <- hms(data$timeleaving)
data$timearriving <- hms(data$timearriving)

data$timeA <- ymd_hms(data$timeA )
data$timeL <- ymd_hms(data$timeL )  

data$timeleaving <- ymd_hms(paste(floor_date(data$timeL, "days"), data$timeleaving))
data$timearriving <- ymd_hms(paste(floor_date(data$timeA, "days"), data$timearriving))
data$late <- data$timeA - data$timearriving 
Edgar Santos
  • 3,426
  • 2
  • 17
  • 29
  • sorry for that. I used this: timearriving <- ymd_hms(paste(floor_date(timeA, "days"), timearriving)) Warning message: All formats failed to parse. No formats found. > late <- timeA -timearriving but it does not work by showing this error as you can see above , I modified it like this because I need to calculate the timeA-timearriving (timeA shows the time the professor should have checked in, whereas timearriving shows us the time he actually checked in.) I need to do this for all the rows and store the calculation in the new column I have created with the name "late". – gtcodes Jun 20 '17 at 21:45
  • this is the code I created the new column to store the calculations: dataset["late"] <- NA dataset$late <- dataset$timeA - dataset$timearriving – gtcodes Jun 20 '17 at 21:47
  • Make sure that the class(timeA) is POSIXct and class(timeleaving) is time or period. – Edgar Santos Jun 20 '17 at 21:48
  • If not try converting them e.g.: timeA <- ymd_hms(timeA ) and timeleaving <- hms(timeleaving ) – Edgar Santos Jun 20 '17 at 21:54
  • I get an error when converting them : timeA <- ymd_hms(timeA ) > timearriving <- hms(timearriving ) Warning message: In .parse_hms(..., order = "HMS", quiet = quiet) : Some strings failed to parse – gtcodes Jun 20 '17 at 22:11
  • Could you please edit your question and add a section of your dataset (say 10 rows), so that we can provide an answer? You could use dput() – Edgar Santos Jun 20 '17 at 22:15