简体   繁体   中英

To find all possible grade pairs for a certain value using R

I have got a problem statement here, and I am stuck to complete it as a whole.

Here is the statement:

Suppose there are 5 possible grades and associated grade points for each of two courses. Suppose the grades are A, B, C, D and F and the associated grade points are 4,3,2,1 and 0 respectively. For a randomly selected student, there are nine possible values of his/her grade point average (GPA) in the two courses and these are (4, 3.5, 3, 2.5, 2, 1.5, 1, 0.5, 0). For example, the value 2.5 arises if the student gets grades (A, D), (B, C), (C, B), and (D, A).

I tried out my level best to write a code for the value of GPA as a function of grades obtained and find all possible grade pairs for a certain value. I could not get the desired output. Can anyone try this out to get the expected output?

Thanks in advance!!

You could use a convenient function that solves this problem in the dplyr package. See details at this link including a series of basic, worked examples:

https://tidyr.tidyverse.org/reference/expand.html

You can solve this problem with a data.frame and a function. First create a dataset for all combination and GPA and than filter the results.


library(tidyverse)

df <- data.frame(grade1 = c("A", "B", "C", "D", "F"),
                 grade2 = c("A", "B", "C", "D", "F")) %>% 
  expand.grid(.) %>%
  data.frame(.) %>%
  mutate(points1 = case_when(grade1 == "A" ~ 4,
                             grade1 == "B" ~ 3,
                             grade1 == "C" ~ 2,
                             grade1 == "D" ~ 1,
                             TRUE ~ 0),
         points2 = case_when(grade2 == "A" ~ 4,
                             grade2 == "B" ~ 3,
                             grade2 == "C" ~ 2,
                             grade2 == "D" ~ 1,
                             TRUE ~ 0),
         GPA = (points1 + points2)/2)


df %>% 
  filter(GPA == 2.5)


  grade1 grade2 points1 points2 GPA
1      D      A       1       4 2.5
2      C      B       2       3 2.5
3      B      C       3       2 2.5
4      A      D       4       1 2.5

Or you put the whole code in a function:

find_combinations <- function(x) {
  df <- data.frame(grade1 = c("A", "B", "C", "D", "F"),
                   grade2 = c("A", "B", "C", "D", "F")) %>% 
    expand.grid(.) %>%
    data.frame(.) %>%
    mutate(points1 = case_when(grade1 == "A" ~ 4,
                               grade1 == "B" ~ 3,
                               grade1 == "C" ~ 2,
                               grade1 == "D" ~ 1,
                               TRUE ~ 0),
           points2 = case_when(grade2 == "A" ~ 4,
                               grade2 == "B" ~ 3,
                               grade2 == "C" ~ 2,
                               grade2 == "D" ~ 1,
                               TRUE ~ 0),
           GPA = (points1 + points2)/2)
  
  
  res <- df %>% 
    filter(GPA == x)
  
  return(res = res)
}

find_combinations(3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM