dimanche 12 août 2018

R IF statement with different selection

Ciao,

I have taken much feedback to create a reproducible example and my coding attempts.

Here is a sample data frame:

df <- data.frame("STUDENT" = 1:10, "test_FALL" = c(0,0,0,0,1,1,1,0,0,NA), "test_SPRING" = c(1,1,1,1,0,0,0,1,1,1), "score_FALL"=c(53,52,98,54,57,87,62,95,75,NA), "score_SPRING"=c(75,54,57,51,51,81,77,87,73,69), "final_SCORE" = c(75,54,57,51,57,87,62,87,73,69))

And sample code:

df$tot_score[df$test_SPRING  == 1] <- df$score_SPRING
df$tot_score[df$test_FALL == 1] <- df$score_FALL

And a second attempt at the code:

df$tot_score1[(df$test_SPRING  == 1 & !is.na(df$test_SPRING))] <- df$score_SPRING
df$tot_score1[(df$test_FALL == 1 & !is.na(df$test_FALL))] <- df$score_FALL

In my dataframe I have when a student took a test (test_SPRING test_FALL) and scores on tests (score_SPRING score_FALL). Basically I want to create the final_SCORE column which I include in the dataframe SUCH THAT if test_SPRING = 1, tot_score = score_SPRING else if test_FALL = 1, tot_score = score_Fall. I am unable to do so and cannot figure it out after many hours. Please offer any advice you may have.

Aucun commentaire:

Enregistrer un commentaire