[R] Reshaping data with xtabs reorders my rows

filip.bielejec at rega.kuleuven.be filip.bielejec at rega.kuleuven.be
Wed Jun 15 13:54:14 CEST 2011


Dnia 2011-06-15, o godz. 12:05:01
Jim Lemon <jim at bitwrit.com.au> napisał(a):

> On 06/15/2011 06:46 PM, filip.bielejec at rega.kuleuven.be wrote:
> > Dear,
> >
> > I have a data frame melted from a list of matrices (melt from
> > reshape package), then I impute some missing values and then want
> > to tabulate the data again to the array (or list, doesn't matter)
> > of matrices form. However when using xtabs function it orders my
> > rows alphabetically and apparently doesn't take "reorder = FALSE"
> > option or anything like it. Is there anything I can do to stop it
> > from doing so?
> >
> > Relevant parts of code:
> >
> > matrices.m<- melt(combined_list)
> >
> > matrices.m_i[is.na(matrices.m_i$value),]$value<- predictions
> >
> > matrices<- xtabs(value ~ location + variable + week, data =
> > matrices.m_i)
> >
> Hi filip,
> Have you tried specifically classing your variables as factors and
> using the "ordered" argument?
> 
> Jim
> 
Dear,

That's exacly what the problem was - the factor levels are by default
in alphabetical order, so I had to fix that by:

levels(matrices.m_i$location) <-
unique(as.character(matrices.m_i$location))

levels(matrices.m_i$variable) <-
unique(as.character(matrices.m_i$variable))

Then xtabs works as advertised.

Cheers, 
filip

-- 
while(!succeed) { try(); }



More information about the R-help mailing list