I have a large dataframe, and am trying to count up the scores of many questions. Here is some sample data.
Q1 = c("apple", "banana", "cider", "muffin", "chocolate")
Q2 = c("orange", "kiwi", "calzone", "cupcake", "cake")
ID = c("P1", "P2", "P3", "P4", "P5")
mydf = data.frame(Q1,Q2,ID)
answer_key = c("apple", "kiwi", "pizza", "dessert", "cake")
I've been trying to use ifelse and %in% for the whole dataframe
mydf = ifelse(mydf %in% answer_key, 1,0)
but it doesn't work, and it returns a vector when I need a dataframe. I just want to replace my values without having to do this for each question because there are many:
mydf$Q1 <-ifelse(mydf$Q1 == "apple", 1, 0)
mydf$Q2 <-ifelse(mydf$Q2 == "kiwi", 1, 0)
mydf[1:2] <- +(mydf[1:2] == answer_key)