Question:
I’m using R to analyze data. I have an ordered grouped time series that shows the brightness of a sample at different times, starting at 0Group | Time | Brightness | Retention |
---|---|---|---|
A | 0 | 100 | NA |
A | 50 | 70 | = 70 /100 |
A | 100 | 20 | = 20/100 |
B | 0 | 90 | NA |
B | 50 | 80 | = 80 /90 |
B | 100 | 50 | = 50/90 |
To calculate retention, I have to divide by the brightness at time 0 for that group. But there are multiple time zeros throughout the table. I tried using a for loop, but due to the length of the data, this takes about 15 seconds to run; I’m looking for more efficient ways.
Thanks for helping ๐
Answer:
You can useifelse
to calculate Retention
on Time
not equal to 0.Data
df <- structure(list(Group = c("A", "A", "A", "B", "B", "B"), Time = c(0L, 50L, 100L, 0L, 50L, 100L), Brightness = c(100L, 70L, 20L, 90L, 80L, 50L)), class = "data.frame", row.names = c(NA, -6L)) [/code]
If you have better answer, please add a comment about this, thank you!