Home > other >  How can I perform different operations in the same column of data frame?
How can I perform different operations in the same column of data frame?

Time:06-16

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 0

Group 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 :)

CodePudding user response:

You can use ifelse to calculate Retention on Time not equal to 0.

library(dplyr)

df %>% 
  group_by(Group) %>% 
  mutate(Retention = ifelse(Time != 0, Brightness/Brightness[Time == 0], NA))

# A tibble: 6 × 4
# Groups:   Group [2]
  Group  Time Brightness Retention
  <chr> <int>      <int>     <dbl>
1 A         0        100    NA    
2 A        50         70     0.7  
3 A       100         20     0.2  
4 B         0         90    NA    
5 B        50         80     0.889
6 B       100         50     0.556

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))
  • Related