Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:05:44+00:00 2026-06-14T04:05:44+00:00

I have a data set with which I create a table, but now I

  • 0

I have a data set with which I create a table, but now I would like the output to be sorted alphabetically. order, sort or sort.list I don’t get to work. Here is an example of what I have.

library(maptools)
data(wrld_simpl)
tab.test <- xtable(subset(wrld_simpl@data, select=c(NAME, REGION)))
head(tab.test)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Sun Nov  4 19:58:19 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rll}
  \hline
 & NAME & REGION \\ 
  \hline
ATG & Antigua and Barbuda &  19 \\ 
DZA & Algeria &   2 \\ 
AZE & Azerbaijan & 142 \\ 
ALB & Albania & 150 \\ 
ARM & Armenia & 142 \\ 
AGO & Angola &   2 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

I would like to be able to sort it by NAME, REGION or even the first column, which is ISO3 and seems to be the default left column. I appreciate your help, including hints to similar questions (I might have overlooked them during my search for a cure).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:05:47+00:00Added an answer on June 14, 2026 at 4:05 am

    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}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of controls bound to data, on which I would like
I have a mysql table field set as time type which stores data in
I have a data set which consists of an ID and a matrix (n
I have the swiss data set provided by R, which has the following form:
I have a large set of data which I access via a generator/iterator. While
I have a set of data points in 3D space which apparently all fall
I have several objects set up in Core Data, one of which is Deck
I'm pretty new to Entity Frameworks. I have a set of Static Data which
I have a query that produces a result like this: The data is sorted
We have an PHP/MySQL application where the users can create their own databases, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.