I'm trying to assign values to different columns, separately for each row, based on lookup values. I'm working in R. Here's a minimal working example:
#Item scores
item1 <- c(NA, 1, NA, 4)
item2 <- c(NA, 2, NA, 3)
item3 <- c(NA, 3, NA, NA)
item57 <- c(NA, 4, 4, 1)
mydata <- data.frame(item1, item2, item3, item57)
#Lookup values based on item score
lookup <- data.frame(score = 1:4, value=c(6, 7, 8, 10))
I have many participants (i.e., rows) assessed with a score on each of many items (i.e., columns). I'd like to create variables in my data frame for the values that are tied to the item scores (based on the lookup table). Here's my desired output:
#Desired output (adding value that is tied to item score to the original data)
desiredOutput <- cbind(mydata,
value1 = c(NA, 6, NA, 10),
value2 = c(NA, 7, NA, 8),
value3 = c(NA, 8, NA, NA),
value57 = c(NA, 10, 10, 6))
I have a fairly large dataset and would like to stay away from loops, if possible. Also, we can skip rows with all NAs, if it's faster to process.