HI, I am trying to analyze data and I have not been able to find the answer anywhere. My data looks like below:
#> sample dataset below
df <- data.frame(
site = c(1, 2, 2, 3, 3, 3, 4),
time.spent = c("05:00", "12:00", "06:43", "02:00", "17:00", "09:45", "06:00"),
emissions = c(1.2, 3.6, 2.4, 9.6, 1.8, 5.3, 1.6))
#> this is what I ran
library(ddply)
emit <- ddply (df, c("site"), summarize,
emission = sum (emissions, na.rm=T),
Time = mean (time.spent, na.rm=T))
what I want to do is get a summary by site on average time and emissions
However, I am not able to run it as the time.spent variable is in character or factor format. If I try to change it to numeric - it does not covert from 06:00 to 6 but instead picks a random number and gives 5, or for 29:00 gives 17.
I have searched and searched - so please help me. even tried strptime package and other stuff. Hoping someone here can help me out now!
Thanks @andresrcs - I will make the data in format you suggested next time. It was my first time posting. I will try to edit it right now too for easier reading for others (thank you).
I tried your code, but the issue is the same. When the Time numbers are converted to numeric they change in my outputs. For example the "05:00" time changes to "0.08333" in numeric. Or 12 changes to 0.2.
I am not sure why it is doing that. Hopefully you can help with this issue. Maybe this is because it is changing it to fractions?
But now I have some of your code to work with. Thanks!
Maybe your problem has to do with the structure of your actual dataset, try to make a reproducible example using dput() for getting the sample data, it would be easier if we could work seen at the same data.
Ok, your data says "0:05" (five minutes) not "05:00" (five hours) as you mentioned, so the result is correct "0:05" (five minutes) is equal to 0.08333 hours
I was assuming that your data is in HH:MM format, could you specify what is the actual format of your data and what are the units of your desired output?
If you want to learn how to post a proper reproducible example (and use dput) check this link
Thanks for the update. Yes, I want the output in minutes, but when you mentioned the format it was in and what its doing. it work. Instead of 3600 I used 60 and it works out. Thanks so much. I really appreciate your help! And will read up dput() etc if I have to post again in future.