-5

I'm very new to using R and am really struggling with the below - any help would be gratefully received.

I need to calculate the total mark from an exam and coursework ( x&y ) and need to use Logical Operators in R to calculate this based on the following criteria.

If exam mark is >=50 then the final mark is 0.2x * 0.7y
If exam mark is <50 > 70 then the final mark is y+10
If exam mark is <50 <70 then the final mark is R.

My problem is that I need to put all 3 criteria above together in one string in R so that whatever value x and y has the 'program' I create will give the corresponding final mark.

I've tried numerous ways of doing this but R just errors everytime. I'm positive it's a coding error (user error) but despite googling; trawling through reference books I just can't get it to work.

I think the problem is I understand how the logical operators work - but not how to get the correct formula for final mark to be carried out if the logical operator gives TRUE and also how to put this in one program

My most recent attempt is as below:

finalmark <- ((y>=50) <- (0.2*x+0.8*y)) |((y<=50 & x>70) <- (y+10)) |((y<=50 & x<70) <-    (y))

I've been trying to get this right for the last 4 days - so if anyone can help me or point me in the correct direction I would be really grateful!

joran
  • 169,992
  • 32
  • 429
  • 468

3 Answers3

2
finalmark <- 
    # test if a condition is true..
    ifelse( 
        # here's the condition..
        y >= 50 , 
        # ..and if it is, set `finalmark` equal to this.
        0.2 * x * 0.7 * y , 
        # ..otherwise, if the condition is false..
        ifelse( 
            # test out this nested condition..
            y < 50 & x > 70 ,
            # and if THAT is true, set `finalmark` equal to this
            y + 10 ,
            # ..otherwise, if the second condition is also false..
            ifelse( 
                # test if this second nested condition is true
                y <= 50 & x < 70 ,
                # and if THAT is true, set `finalmark` equal to this
                y ,
                # otherwise, set `finalmark` equal to MISSING
                NA

    # close all of your parentheses
    # out to the same level as before.
            )
        )
    )
Anthony Damico
  • 5,779
  • 7
  • 46
  • 77
0

As one line (assuming you want your third condition to output y as you do in your code attempt):

finalmark <- ifelse(y>=50, 0.2*x+0.8*y, ifelse(x>70, y+10, y))
Blue Magister
  • 13,044
  • 5
  • 38
  • 56
  • Hi, Thank-you for your answer. This works for if I could use ifelse but the criteria of the questions asks for a solution that does not use ifelse. Do you have a suggestion of how that could be done? This is where I'm having the problems – Tanya Kiddle Feb 06 '13 at 11:35
  • Without some sample data and output, I cannot tell what the issue is. – Blue Magister Feb 06 '13 at 15:38
0

It works with just one ifelse command:

finalmark <- ifelse(y >= 50, 0.2 * x + 0.8 * y, y + 10 * (x > 70))
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168