I want to add "2020/01' to all the datasets so that I can calculate the time difference with library(lubridate) ymd_hms function.
The below is test row data but original data contains "date HH:MM:SS" and I want to calculate the time difference for those data. Since right now i can't find the function that allows to calculate with this format D HH:MM:SS, I want to add YYYY/MM/ so that i can use ymd_hms to get the time difference.
Above is the sample data and currently they are character type not numeric so I also need to change to numeric but as.numeric brings to NA for some reasons.
Do you have some ideas how we can
1 convert to numeric
2 add data to get time difference (we do not have to need to add data 2020/01/ if we can get time difference with the above datasets. But since some data the date is next day and brings negative value if we subtract the data.)
If the data are in a matrix, I would change it to a data frame to do the transformations. You can change it back to a matrix if you need the final object to be a matrix.
For the subtraction, do you need to subtract a single value from every value or is a different value used for every case?
Thank you for the advise. But since the original data itself has more than 10k rows I do not think it would be ideal to do transformations.....
For the subtraction I am trying to subtract a different value for every case with combination of time1 , time2, time3 and time4.
One way I was trying to achieve on transforming to num is change all columns to num by using below #get data
time <- read.csv("pasting original excel file name" #remove date
time$time1=substr(time$time1,4,11) #change to num
time$time1 <- as_hms(time$time2)
and do the same for all columns (time1 till time4)
and then subtract and this brings difference but the units is seconds and also they do not consider when the date has changed as we removed date on the first place.
Having 10000 rows is not a problem. The code below invents some data with four columns and 12000 rows. It runs with no problem on my little laptop. Give it a try.