Sort R data.frame by column

Load in your data and convert to a data.frame. I assume you have headers in your file, if you don’t change the True to False in the following:

data <- read.table("myfile.txt", header=True)

If you don’t want to keep typing data$column_name

attach(data)

To list the names of the columns, for reference

names(data)

To sort the data.frame by a column, in this case dmFactorRMS

data[order(data$dmFactorRMS),]

If you’ve bothered to bind data you can do

data[order(dmFactorRMS)]

This won’t modify your input, therefore you will need to create a new varaible

sorted_data <- data[order(dmFactorRMS)]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s