I recently used this function at work to get last performance on an item. That is, diplomates in our program take spaced assessments, with repetition of items (same content, but slightly different item wording) that they answered incorrectly. If they got the initial item correct, they may never see a related item again (although over time they will), but if they were incorrect, they may get related items once or more times to see if they're learning. So we have some people who will have a missing value for the repeated item and others who will have correct/incorrect. The coalesce function allowed me to get their final performance on item content by telling it to first check the repeated item column and then, if that's missing, to look at the initial item column.
How can we use the coalesce function for the reading dataset? I noticed that for 17 books in the dataset, I forgot to supply a rating, so they're showing up as 0. Let's select those titles, and I can figure out how I'd like to rate them.
library(tidyverse)
reads2019 <- read_csv("~/Downloads/Blogging A to Z/SarasReads2019.csv", col_names = TRUE)
unrated <- reads2019 %>% filter(MyRating == 0)
list(unrated$Title)
## [[1]] ## [1] "Bird Box (Bird Box, #1)" ## [2] "Elevation" ## [3] "Glinda of Oz (Oz, #14)" ## [4] "It" ## [5] "Just Evil (Evil Secrets Trilogy, #1)" ## [6] "Oryx and Crake (MaddAddam, #1)" ## [7] "Precarious Pasta (Cozy Corgi Mysteries, #14)" ## [8] "Redshirts" ## [9] "Rinkitink in Oz (Oz, #10)" ## [10] "Summerdale" ## [11] "The 5 Love Languages: The Secret to Love That Lasts" ## [12] "The Long Mars (The Long Earth, #3)" ## [13] "The Magic of Oz (Oz, #13)" ## [14] "The Marvelous Land of Oz (Oz, #2)" ## [15] "The Scarecrow of Oz (Oz, #9)" ## [16] "The Tin Woodman of Oz (Oz, #12)" ## [17] "Tik-Tok of Oz (Oz, #8)"
So now I want to generate a variable with my new ratings, and merge it into my reads dataset.
unrated$NewRating <- c(3, 5, 4, 4, 2, 4, 4, 5, 4, 3, 3, 4, 4, 5, 3, 3, 4)
unrated <- unrated %>% select(Book.ID, NewRating)
reads2019 <- reads2019 %>% left_join(unrated, by = "Book.ID")
Finally, we'll create our final ratings variable by coalescing the two ratings variables. I want it to first look at the new rating column, so it can capture those updates. For rows missing that variable (because I already rated them), it will look next at the original rating variable.
reads2019 <- reads2019 %>% mutate(FinalRating = coalesce(NewRating, MyRating))
table(reads2019$FinalRating)
## ## 2 3 4 5 ## 2 9 51 25
No more 0s!
No comments:
Post a Comment