2012-04-17 183 views
1

我有一個看起來像這樣的R數據幀:R數據幀轉換?

Country  Property Value 

Canada  Capital Ottawa 
Canada  Population 38 
Canada  Language1 French 
Canada  Language2 English 
United States Capital Washington 
United States Population 280 
United States Language1 English 
United States Language2 NA 

我想重新安排這使得它看起來像這樣:

Country   Capital  Population Language1 Language2 

Canada   Ottawa  38   French English 
United States Washington 280  English NA 

有沒有辦法做到這一點的轉型? 謝謝。

+0

看看http://stackoverflow.com/questions/1533493/reshaping-data-frame-in-r – 2012-04-17 20:34:28

回答

4

按照保羅Hiemstra的評論:

reshape2包的dcast將很好地做到這一點:

dcast(data=yourdataframe, Country~Property, value.var='Value') 

如果你已經在那裏了重複值,雖然它會嘗試他們使用length作爲聚集默認,這不是你想要的!