My df "temp" looks like the first four columns of the following:
ID OBS NUMER DENOM RATIO
1 1 26 NA 1.5
1 2 10 NA 0,5556
1 3 18 18 1
1 4 51 26.333 1,461
2 1 17 NA 0,2
I am trying to add the fifth column by dividing the group's values for the column NUMER by the group's first value of DENOM that is not NA. How do I do this?
temp %>%
group_by(ID) %>%
mutate(RATIO = NUMER/first(DENOM[!is.na(DENOM)]))
Running this command however, gives me the following error message:
Error in mutate_impl(.data, dots) : Column RATIO
must be length 2 (the group size) or one, not 0.
I've also tried
temp %>%
group_by(ID) %>%
mutate(RATIO = NUMER/first(.$DENOM[!is.na(.$DENOM)]))
but in this case, it seems to divide by the the first value of the entire DENOM column, instead of the group's first value for that column. Please help! :(