Hello dear R-Forum,
I have a question regarding converting a character variable to a numeric variable so I can use it later in my analyses.
I have measured the duration of certain behaviors of individuals in an experiment. These are in the following form: hour:minutes:seconds.milliseconds (E.g. 00:00:03.792).
I want to be able to add up all the times for each subject at the end.
My data set (data1) looks something like this:
So I want to convert the last two columns that are in character to a numeric variable.
I have already tried a few things but unfortunately always without success.
My attempts so far:
1st attempt:
as.numeric(sub(":", ".", substr(data1$stress_duration,1,4365)), na.rm=TRUE)
--> Nothing happens with this command except a small warning message:
"NAs introduced by coercion"
2nd attempt:
data1$stress_duration <- as.numeric(sub("^(\d+):(\d+).*", "\1.\2", data1$stress_duration))
--> here it shows me that the variable is now numeric, but all times are set to zero
3rd attempt:
data1$stress_duration<-as.numeric(data1$stress_duration, na.rm=TRUE)
--> again I get a message that the variable is now numeric, but all times are set to NA.
4th attempt:
data1$stress_duration <- strptime(data1$stress_duration, format = "%H:%M:%OS")
options(digits.secs = 3)
--> here now also a date is inserted and also with this format I again can't sum up
5th attempt:
data1$stress_duration$gsub("*\:[0-9]", ".", data1$stress_duration)
--> here also nothing happens except an error message:
"This error occurs when you attempt to access an element of an atomic vector using the $ operator."
Does anyone of you have a solution?
Thanks in advance
Sarah