[R] Coerce rownames to factor for ordering

LCOG1 jroll at lcog.org
Thu Nov 29 20:19:20 CET 2012


Hi all, 
   I think this might be an easy one but I cannot think of a way to do this
other than what I am currently attempting.  I simply want to sort my data
frame's rownames by a defined vector so that the plots I generate from that
vector are in the desired order 

Consider the test data below:

#Create test data	
	DataToPlot.. <- data.frame("Location1" = c(100,200),"Location2" =
c(200,400))
	#Name rowns
	rownames(DataToPlot..) <- c("Fall","Summer")
	#Attempt to coerce row names to factors 
	rownames(DataToPlot..) <- as.factor(rownames(DataToPlot..))
	#Attenmpt to sort rownames by defined vector
	rownames(DataToPlot..)<- reorder(as.factor(rownames(DataToPlot..)),
new.order = c("Summer","Fall","Winter","Spring"))
		
The rownames do not reorder nor do they remain factors.  

Perhaps I can just sort these in the plot?  Not sure whats easier/possible?

barchart(DataToPlot..SeCl , horizontal = FALSE, ylab = "Average Weekday
Counts",
			group = colnames(DataToPlot..SeCl ), col =
Colors.[rownames(DataToPlot..SeCl)] )
		

Thanks for any help.

Josh



--
View this message in context: http://r.789695.n4.nabble.com/Coerce-rownames-to-factor-for-ordering-tp4651330.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list