Search code examples
rdataframetagsreshapeassign

Assign grading based on available data in R data.frame?


I have the following two data.frame. I want to get the Grade from D1 and Assign to the Pts in D. Once the operations completes the D data.frame should have a columns for Pts,Val, and Grade without C or Good Grade.

D <- data.frame(Pts= c("A","B","D"), Val = c(3.5,4,5))
D1 = data.frame(Pts = c("A","B","C", "D"), Grade = c("Ugly", "Bad", "Good", "Excellent"))

D$Grade <- D1$Grade

Solution

  • We can use a join in data.table by joining on by 'Pts' column and assign (:=) the 'Grade' from D1 to D

    library(data.table)
    setDT(D)[D1, Grade := Grade, on = .(Pts)]
    

    -output

    D
       Pts Val     Grade
    1:   A 3.5      Ugly
    2:   B 4.0       Bad
    3:   D 5.0 Excellent
    

    In base R, can do the same with merge or more faster match

    D$Grade <- D1$Grade[match(D$Pts, D1$Pts)]
    

    The line of code

    D$Grade <- D1$Grade
    

    wouldn't work due to multiple reasons

    1. The number of rows are not the same
    2. Even if the number of rows are same, the 'Pts' in both datasets may not be matching elementwise