#question I have the following data set: 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) #I'm trying to convert the data set into a contingency table and then back into a regular data frame: xtabdata<-as.data.frame.matrix(xtabs(EstimatedQuantity~Date+ScowNo,data=dataset), row.names=(dataset$Date),optional=F) #I'm trying to keep the row names (in xtabsdata) as the dates. #But the row names keep coming up as integers. #How can I preserve the row names as dates when #the table is converted back to a data frame? -- View this message in context: http://r.789695.n4.nabble.com/Keeping-row-names-when-using-as-data-frame-matrix-tp4667344.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2013-May-17 19:05 UTC
[R] #Keeping row names when using as.data.frame.matrix
On May 17, 2013, at 9:46 AM, Tim wrote:> #question I have the following data set: > > 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) > > #I'm trying to convert the data set into a contingency table and then back > into a regular data frame: > > > xtabdata<-as.data.frame.matrix(xtabs(EstimatedQuantity~Date+ScowNo,data=dataset), > row.names=(dataset$Date),optional=F) > > #I'm trying to keep the row names (in xtabsdata) as the dates. > #But the row names keep coming up as integers. > #How can I preserve the row names as dates when > #the table is converted back to a data frame?It's a factor-problem (see the FAQ ?. read all of section 7, i'm too lazy to look it up again.): xtabdata<-as.data.frame.matrix(xtabs(EstimatedQuantity~Date+ScowNo,data=dataset), row.names=sort( unique( as.character(dataset$Date))), optional=F)> > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Keeping-row-names-when-using-as-data-frame-matrix-tp4667344.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA
Hi, library(plyr) res<-dcast(dataset,Date~ScowNo,sum,value.var="EstimatedQuantity") ?rownames(res)<- res[,1] res[,-1] #???????? 3002 4001 4002 BR 8 #9/7/2010 2772 3535 6763 6685 #9/8/2010??? 0 3305??? 0??? 0 A.K. ----- Original Message ----- From: Tim <tfox at mde.state.md.us> To: r-help at r-project.org Cc: Sent: Friday, May 17, 2013 12:46 PM Subject: [R] #Keeping row names when using as.data.frame.matrix #question I have the following data set: 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) #I'm trying to convert the data set into a contingency table and then back into a regular data frame: ? ? ? ? xtabdata<-as.data.frame.matrix(xtabs(EstimatedQuantity~Date+ScowNo,data=dataset), ? ? ? ? row.names=(dataset$Date),optional=F) #I'm trying to keep the row names (in xtabsdata) as the dates. #But the row names keep coming up as integers. #How can I preserve the row names as dates when #the table is converted back to a data frame? -- View this message in context: http://r.789695.n4.nabble.com/Keeping-row-names-when-using-as-data-frame-matrix-tp4667344.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.