filip.bielejec at rega.kuleuven.be
2011-Jun-15 08:46 UTC
[R] Reshaping data with xtabs reorders my rows
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) -- while(!succeed) { try(); }
filip.bielejec at rega.kuleuven.be
2011-Jun-15 11:54 UTC
[R] Reshaping data with xtabs reorders my rows
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(); }