【发布时间】:2015-03-31 23:09:01
【问题描述】:
我有一个包含 productID、Seller1Name、Seller1Price、Seller2Name、Seller2Price 的数据框,如下所示。 表 (DF) 按 productID 是唯一的:
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
我尝试使用 reshape 包,但结果很奇怪:
Output <-melt(DF, Id = c("ProductID"))
有没有更好的方法来做到这一点?
【问题讨论】:
-
这是一个简单的
reshape操作 -reshape(dat, idvar="ProductID", direction="long", varying=list(c(2,4),c(3,5)), v.names=c("Seller","Price") ) -
...如果您不想依赖 data.frame 中变量的位置,请使用
varying=lapply(c("Name","Price"),grep,x=names(dat))或类似名称。