I have a dataframe that kind of looks like this:
ID Day Value
A 1 1.4
A 2 3.4
A 3 5.6
A 4 6.7
B 1 2.3
B 2 3.4
B 2 3.5
C 1 2.3
C 2 4.6
C 3 6.8
I want to add a column "Difference to previous day". As you can see, not all Objects in the table have values until the last day and sometimes there are multiple measurements for one day (B2), which should be summarized by taking the mean.
What is a good way to do this in R? I'm using a loop at the moment to go through each row and find the matching ones from the previous day, but it takes forever and doesn't feel like you're supposed to do it that way.
Bonus Question: Measurements were not always taken at the same time of day, so in truth the Day column is a float (2.4553 days from the start of the experiment). It's not necessary to normalize the differences in any way since the resulting diff will end up in a plot with Day[Float] at the X axis, but it would be nice if there'd be a solution that doesn't rely on Day - 1 for finding the previous entry.