I am trying to add a column of data where the value is attributed to a different row with one id that is the same, and the other id is not the same. The data is below.
class_id student score other_score
1 23 87 93
1 27 93 87
2 14 77 90
2 19 90 77
The other_score column is what I am looking to achieve, given the first three coulmns. I have already tried:df$other_score = df[df$class_id == df$class_id & df$student != df$student,]$score
I might be under complicating it but if there is always just two kids, sum after group by then remove score
library(dplyr)
output = df %>%
group_by(class_id) %>%
mutate(other_score = sum(score)-score)
output
# A tibble: 4 x 4
# Groups: class_id [2]
class_id student score other_score
<dbl> <dbl> <dbl> <dbl>
1 1 23 87 93
2 1 27 93 87
3 2 14 77 90
4 2 19 90 77
One option would be to use lead
and lag
, and to retain the non NA
value, whatever that might be:
library(dplyr)
output <- df %>%
group_by(class_id) %>%
mutate(other_score <- ifelse(is.na(lead(score, order_by=student)),
lag(score, order_by=student),
lead(score, order_by=student)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With