I have two data frames where the data are connected via the "ID" label as follows:
test_frame1<- data.frame(
ID = c("A","B","C"),
ElementType1 = c(1,6,1),
ElementType2= c(4,4,5),
ElementType3 = c('',6,1),
Notes = c("Something random","","Something else random")
)
test_frame2<-data.frame(
ID = c("A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C"),
Syllable = c(1,1,2,2,3,3,1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3),
ElementID = c(1,2,1,2,1,2,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
)
I want to add a new column to test_frame2 to represent the element type, as described by the middle columns in test_frame1. I want these element types to repeat for each different syllable as follows:
desired_frame<-data.frame(
ID = c("A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C"),
Syllable = c(1,1,2,2,3,3,1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,3,3,3),
ElementID = c(1,2,1,2,1,2,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3),
ElementType= c(1,4,1,4,1,4,6,4,6,6,4,6,6,4,6,1,5,1,1,5,1,1,5,1)
)
I first transposed test_frame1 so that the element types are in the same column, with ID being the column names. Then I tried to separate test_frame2 into a list grouped by ID and Syllable. At this point I hope to merge, cbind, or in some other way paste the values in the element type columns from test_frame1 to the corresponding list item for that ID into test_frame2. It seems like a 'for loop' might help with this, too. I am struggling, though, to find the right function to merge a list and multiple columns based on one part of the list elements title. Any ideas on a simply way to go about this?