I'm trying to use the gather function to combine multiple rows and lengthen my wide data. Example data below:
User ID Book 1 Book 1_YN Book 2 Book 2_YN Book 3 Book3_YN Book 4 Book 4_YN 1 ABC Y XYZ N LMN Y 2 XYZ Y DEF Y 3 ABC N XYZ Y TUV N HIJ Y
Ideally, I want the data to look like the below table so I can summarize info about the books:
User ID Book_Num Book Book_YN 1 Book 1 ABC Y 1 Book 2 XYZ N 1 Book 3 LMN Y 2 Book 1 XYZ Y 2 Book 2 DEF Y 3 Book 1 ABC N 3 Book 2 XYZ Y 3 Book 3 TUV Y 3 Book 4 HIJ Y
When I try to use column indexing in the gather function...
data_clean <- gather(data, Book_Num, Book, data[c(2,4,6,8)]
I get the following error:
"Error: data[c(2,4,6,8)]
must evaluate to column positions or names, not a list"
Anyone know what that error means and/or if there is a better way to handle this task?
*Edited to change images to tables
dput
and not as images. Please read the info about how to ask a good question and how to give a reproducible example – Ronak Shah