I'm having issue trying to convert rows into columns and then getting only the latest record (using timestamp). Here is my data set:
Below is the code to generate this data set:
df <- data.frame(id = c("123||wa", "123||wa", "123||wa", "223||sa", "223||sa", "223||sa", "123||wa"),
questions = c("dish", "car", "house", "dish", "house", "car", "dish"),
answers = c("pasta", "bmw", "yes", "pizza", "yes", "audi","ravioli" ),
timestamp = c("03JUL2014:15:38:11", "07JAN2015:15:22:54", "24MAR2018:12:24:16", "24MAR2018:12:24:16",
"04AUG2014:12:40:30", "03JUL2014:15:38:11", "05FEB2018:17:23:16"))
The desired output is:
code that generated the output:
output <- data.frame(id = c("123||wa", "223||sa"), dish = c("ravioli", "pizza"),
car = c("bmw", "audi"), house = c("yes", "yes"))
NOTE: As you can see in the original data set, there were multiple rows for the id field. More importantly, there were two rows for id '123||wa' regarding their favourite dish but only their latest answer is wanted in the final output.
Any help would be greatly appreciated. Thanks