我有一個如下所示的productID,Seller1Name,Seller1Price,Seller2Name,Seller2Price的數據框。 表(DF)是唯一的由產品編號:將兩個字段轉換爲R中的一個唯一鍵值
ProductID Seller1Name Seller1Price Seller2Name Seller2Price
1 A $1 X $3
2 B $3 Y $6
3 C $2 Z $1
所需的輸出應爲DF:
ProductID Seller Price
1 A $1
1 X $3
2 B $3
2 Y $6
3 C $2
3 Z $1
我使用重塑包嘗試,但結果是質樸:
Output <-melt(DF, Id = c("ProductID"))
有沒有更好的方法來做到這一點?
這是一個簡單的'reshape'操作
merged.stack
- ' reshape(dat,idvar =「ProductID」,direction =「long」,vary = list(c(2,4),c(3,5)),v.names = c(「Seller」,「Price」)) ' – thelatemail 2015-03-31 23:55:41... a如果您不想依賴data.frame中變量的位置,請使用'vary = lapply(c(「Name」,「Price」),grep,x = names(dat))'或類似的名稱。 – thelatemail 2015-04-01 01:50:45