lundi 18 avril 2016

comparing 2 columns in a data-frame and creating a new column in existing data frame in R

I have "merged" information from 2 data frames,& I have compared 2 columns answer & selected_answer I got the values true & false by doing,

rt$is_correct <- dt$correct_answer==dt$selected_answer 

but I want to add another column based on the values of final_ans if it's true then want to display 3 or if false then display 0 below is the example . this below is the code.

  dt = within(dt, {
  score = ifelse(is_correct == TRUE, 3,3)
  score = ifelse(is_correct == FALSE, 0,0)
})

#it displays only 0 for all rows.. how do edit the code to get exact values of true=3 & false=0

    Q_id c1  c2 c3 c4 answer stu_id selected_answer  final_ans score
     2   1   2  3   4   2    82       3                false     0
     3   1   2  3   4   2    58       3                false     0
     3   1   2  3   4   2    83       2                true      0
     3   1   2  3   4   2    100      3
     4   1   2  3   4   1    79       3
     5   1   2  3   4   1    100      3
     7   1   2  3   4   1    54       3
     7   1   2  3   4   1    92       3
     8   1   2  3   4   3    56       2

Aucun commentaire:

Enregistrer un commentaire