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 8835977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:23:11+00:00 2026-06-14T09:23:11+00:00

Possible Duplicate: How to sort a dataframe by column(s) in R I was just

  • 0

Possible Duplicate:
How to sort a dataframe by column(s) in R

I was just wondering if some one could help me out, I have what I thought should be a easy problem to solve.

I have the table below:

SampleID           Cluster

R0132F041p          1

R0132F127           1

R0132F064           1

R0132F068p          1

R0132F015           2

R0132F094           3

R0132F105           1

R0132F013           2

R0132F114           1

R0132F014           2

R0132F039p          3

R0132F137           1

R0132F059           1

R0132F138p          2

R0132F038p          2

and I would like to sort/order it by Cluster to get the results as below:

SampleID    Cluster

R0132F041p  1

R0132F127   1

R0132F064   1

R0132F068p  1

R0132F105   1

R0132F114   1

R0132F137   1

R0132F059   1

R0132F015   2

R0132F013   2

R0132F014   2

R0132F138p  2

R0132F038p  2

R0132F094   3

R0132F039p  3

I have tried the following R code:

data<-read.table('Table.txt', header=TRUE,row.names=1,sep='\t')

data <- data.frame(data)
data <- data[order(data$Cluster),]
write.table(data, file = 'OrderedTable.txt', append = TRUE,quote=FALSE, sep = '\t', na ='NA', dec = '.', row.names = TRUE, col.names = FALSE)

and get the following output:

1   1

2   1

3   1

4   1

5   1

6   1

7   1

8   1

9   2

10  2

11  2

12  2

13  2

14  3

15  3

Why have the SampleIDs been replaced by the numbers 1-15 and what do these numbers represent, I have read the ?order() page however this seems to explain sort.list better than order() if any one could help me out on this I would be very grateful.

  • 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-14T09:23:12+00:00Added an answer on June 14, 2026 at 9:23 am

    The short answer is you did it perfectly. You just are having some difficulty with reading and writing files. Going through your code:

    data<-read.table('Table.txt', header=TRUE,row.names=1,sep='\t')
    

    The above line is reading in your data fine, but the row.names=1 told it to use the first column as names for rows. So now your SampleIDs are row names instead of being their own column. If you type data or head(data) or str(data) immediately after running this line, this should be clear. Just omit that row.names argument and it will read properly.

    data <- data.frame(data)
    

    You don’t need this above line because read.table() produces a dataframe. You can see that with str(data) as well.

    data <- data[order(data$Cluster),]
    

    The above line is perfect.

    write.table(data, file = 'OrderedTable.txt', append = TRUE,
       quote=FALSE, sep = '\t', na ='NA', dec = '.', row.names = TRUE, 
       col.names = FALSE)
    

    Here you included the argument col.names = FALSE which is why your file doesn’t have column names. You also don’t need/want append=TRUE. If you look at help(write.table), you see it is “only relevant if file is a character string”. Here it seems to make the file write without ending the last line, which would likely cause any later read.table() to complain.

    The numbers 1-15 in your result look like row numbers. You don’t explain how you look at the resulting file, so I cannot be sure. You likely read your file in a way that doesn’t parse the row.names and is showing row numbers instead. If you make certain your SampleIDs column does not get assigned to be names of rows, you’ll probably be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Possible Duplicate: How to sort a dataframe by column(s) in R Here is the
Possible Duplicate: How to sort a dataframe by column(s) in R I am trying
Possible Duplicate: How to sort a dataframe by column(s) in R everyone: I want
Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
Possible Duplicate: Natural Sort Order in C# I have a list with a lot
Possible Duplicate: mysql custom sort I have an array of IDs: $ids = array(5,
Possible Duplicate: Sort ArrayList of custom Objects by property I have an arrayList of
Possible Duplicate: Sort by key of dictionary inside a dictionary in Python I have
Possible Duplicate: How to sort a NSArray alphabetically? Hi all, I have an array

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.