I have another tricky task that I cannot master at the moment. It deals with dataframes in R.
Say I have a data frame looking like:
original = data.frame(Male = c(rep(1,3),rep(2,4),rep(3,2)),
SongNumber = c(1,2,3,1,2,3,4,1,2),
SongType = c("16a","16b","17a","24a","24b","25d","24f","5e","5e"),
Start = c(0.5,16.1,24.2,0.9,10.1,18.9,0.7,0.6,12.2),
RecordFile = c(rep("A1",3),rep("B1",3),"B2",rep("C1",2)))
original
and another data frame containing the particular syllable order of each song type:
additional = data.frame(SongType = c("16a","16b","17a","24a"),
Syll1 = c(4,4,3,16),
Syll2 = c(4,4,3,16),
Syll3 = c(84,84,3,3),
Syll4 = c(3,3,3,16),
Syll5 = c(16,16,3,3),
Syll6 = c(16,16,NA,4),
Syll7 = c(NA,16,NA,NA),
Syll8 = c(NA,16,NA,NA),
Syll9 = c(NA,3,NA,NA),
Syll10 = c(NA,1,NA,NA))
additional
what I would like is to now insert the syllable order as a column in the previous data frame. The final result should look like this:
aim = data.frame(Male = c(rep(1,21),rep(2,9),rep(3,2)),
SongNumber = c(rep(1,6),rep(2,10),rep(3,5),rep(1,6),2,3,4,1,2),
SongType = c(rep("16a",6),rep("16b",10),rep("17a",5),rep("24a",6),"24b","25d",
"24f","5e","5e"),
Start = c(rep(0.5,6),rep(16.1,10),rep(24.2,5),rep(0.9,6),10.1,18.9,0.7,0.6,
12.2),
RecordFile = c(rep("A1",21),rep("B1",8),"B2",rep("C1",2)),
SyllOrder = c(4,4,84,3,16,16,4,4,84,3,16,16,16,16,3,1,3,3,3,3,3,16,16,3,16,3,4,
NA,NA,NA,NA,NA))
aim
So far, I do not see how functions such as merge can help: merge only add columns of dataframe2 to dataframe1 based on a common column between two data frames. It does not force dataframe1 to add rows accordingly!
additional
into a long format could not be found in the question [How to join (merge) data frames (inner, outer, left, right)?] – KrisAnathema