2015-03-31 23 views
2

我有一個如下所示的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")) 

有沒有更好的方法來做到這一點?

+1

這是一個簡單的'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

+0

... a如果您不想依賴data.frame中變量的位置,請使用'vary = lapply(c(「Name」,「Price」),grep,x = names(dat))'或類似的名稱。 – thelatemail 2015-04-01 01:50:45

回答

3

data.table v1.9.5,目前開發人員版本,melt爲data.tables已經獲得了一個新的功能 - 能夠融到多列..

require(data.table) ## v1.9.5+ 
ans = melt(setDT(DF), measure=patterns("Name$", "Price$"), 
       value.name=c("seller", "price")) 

我們只是提供列而熔化組合在一起作爲measure.vars的參數列表。

現在,你可以刪除variable列和重新排列如下:

setorder(ans[, variable := NULL], ProductID)[] 
# ProductID seller price 
# 1:   1  A $1 
# 2:   1  X $3 
# 3:   2  B $3 
# 4:   2  Y $6 
# 5:   3  C $2 
# 6:   3  Z $1 

HTH

+0

這實現了我的目標! – BlackHat 2015-03-31 23:29:11

1

另一種選擇是從splitstackshape

library(splitstackshape) 
res <- merged.stack(DF, var.stubs=c('Name', 'Price'), sep='var.stubs', 
        atStart=FALSE)[,2:= NULL][] 
setnames(res, 2, 'Seller') 
res 
# ProductID Seller Price 
#1:   1 A $1 
#2:   1 X $3 
#3:   2 B $3 
#4:   2 Y $6 
#5:   3 C $2 
#6:   3 Z $1 
相關問題