Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a column based on unlike value in another column

Tags:

r

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

like image 545
theofficefan12 Avatar asked Dec 31 '22 20:12

theofficefan12


2 Answers

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

like image 68
enongad Avatar answered Jan 08 '23 02:01

enongad


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)))
like image 30
Tim Biegeleisen Avatar answered Jan 08 '23 02:01

Tim Biegeleisen