使用as.data.frame.matrix时保留行名称

时间:2013-05-17 17:01:53

标签: r dataframe

我有以下数据集:

Date<-c("9/7/2010","9/7/2010","9/7/2010",
        "9/7/2010","9/7/2010","9/7/2010","9/8/2010")
EstimatedQuantity<-c(3535,2772,3279,3411,3484,3274,3305)
ScowNo<-c("4001","3002","4002","BR 8","4002","BR 8","4001")

dataset<- data.frame(EstimatedQuantity,Date,ScowNo)

我正在尝试将数据集转换为列联表,然后再转换回常规数据框:

xtabdata<-as.data.frame.matrix(xtabs(EstimatedQuantity~Date+ScowNo,
                                     data=dataset),
                               row.names=(dataset$Date),optional=F)

1 个答案:

答案 0 :(得分:5)

这是你想要的吗?

as.data.frame(unclass(xtabs(EstimatedQuantity~Date+ScowNo,data=dataset)))