This has nothing to do with xtable
as far as I can see
Given that wrld_simpl
is a SpatialPolygonsDataFrame
I think it will be easier to work with a copy of the data
slot that attempt to modify an S4
object.
The plyr
package has a nice function arrange
which makes ordering easy. It is not clear whether you want them arranged by NAME
and then REGION
so I will give the example of ordering by NAME
alone
wrld_data <- wrld_simpl@data
library(plyr)
arranged_data <- arrange(wrld_data, NAME)
# this was nice syntax for
# wrld_data[order(wrld_data$NAME),]
# subset colums
subset_data <- subset(arranged_data, select = c(NAME, REGION))
head(subset_data)
NAME REGION
1 Aaland Islands 150
2 Afghanistan 142
3 Albania 150
4 Algeria 2
5 American Samoa 9
6 Andorra 150
And just to show your issue was not xtable
related
.x <- xtable(subset_data)
head(.x)
% latex table generated in R 2.15.2 by xtable 1.7-0 package
% Mon Nov 05 13:03:27 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlr}
\hline
& NAME & REGION \\
\hline
1 & Aaland Islands & 150 \\
2 & Afghanistan & 142 \\
3 & Albania & 150 \\
4 & Algeria & 2 \\
5 & American Samoa & 9 \\
6 & Andorra & 150 \\
\hline
\end{tabular}
\end{center}
\end{table}