0

Below I have code to merge two data frames and assign values 3 and -1,

candidate_score<-merge(check7,anskey,by='Question.ID')

  candidate_score$correct <- candidate_score$Selected.Option.ID == candidate_score$Correct.Option.ID

  candidate_score$score <- 
        ifelse(candidate_score$correct== TRUE, 3,
                              ifelse(candidate_score$correct== FALSE, -1, ifelse(candidate_score$Correct.Option.ID == Full Marks ,3,NA)))

I am having student data, when am assigning marks 3,-1 according to candidate_score$score data frame its shown below the marks 3 is not assigned to Full Marks in correct.option.idcolumn according to my candidate_score$score code how can i achieve my desired output?

i want to also assign 3 marks wherever correct.option.id has Full Marks.

Apache11
  • 189
  • 11

1 Answers1

0

The second ifelse has 4 arguments. You need to decide whether the consequent of that conditional should be -1 or NA. There's no way of determining your intent from the material presented. Best would be a sample dataframe or vector and some description.

It's often easier to find errors if you insert space after commas and surround assignment operators with spaces as well. I've tried to edit the code in a more structured manner.

Responding to the request for clarification... this is the second ifelse call::

ifelse(candidate_score$correct== FALSE, # arg 1 (the condition)
       -1 ,                             # arg 2 (the consequent)
        NA,                             # arg 3 (should be the alternative)
                 # and the following fourth argument causes an error.
        ifelse(candidate_score$Correct.Option.ID == Full Marks ,3,NA))

Still not entirely clear what logical tests are to be applied but perhaps you want this:

candidate_score$score <- 
 ifelse(candidate_score$correct== TRUE | candidate_score$Correct.Option.ID == 'Full Marks', 
        3,
        ifelse( candidate_score$correct== FALSE, -1,,NA))

You should also realize that the ==TRUE parts are not needed, since TRUE has the same value as TRUE==TRUE, and FALSE has the same value as FALSE==TRUE,

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I suspect that you want to drop that third argument, but since you have not yet provided an example as requested, that's sheer speculation. – IRTFM Jun 17 '16 at 06:58
  • Again .... since you have neither described the desired result nor provided a working example, discussions about the results of the process seem entirely speculative. You should not be providing code edits in comments, but rather using the [edit] function to correct your question body. – IRTFM Jun 17 '16 at 07:07
  • I don't understand how the ifelse condition regarding 'Full Marks' will ever be evaluated. You have candidate_score$correct tested for either TRUE or FALSE and so that exhaust the possibilities. – IRTFM Jun 17 '16 at 07:52