我還有一個棘手的任務,我目前無法掌握。它與dataframes成交在R.如何轉換並在另一個數據幀中包含數據幀
說我有一個數據幀看起來像:
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
,並含有每首歌曲類型的音節順序另一個數據幀:
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
我希望現在插入音節順序作爲前一個數據框中的一列。最後的結果應該是這樣的:
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
到目前爲止,我沒有看到的功能,如合併如何幫助:僅合併的dataframe2列添加到dataframe1基於兩個數據幀之間的公共列。它不會強制dataframe1相應地添加行!
好吧,@Thierry提供的答案是首先將'additional'轉換爲長格式無法在[如何連接(合併)數據幀(內部,外部,左側,右側)?] – KrisAnathema