Skip to content

Instantly share code, notes, and snippets.

@nickpettican
Created October 26, 2015 16:32
Show Gist options
  • Save nickpettican/81850826a82f36c0eda8 to your computer and use it in GitHub Desktop.
Save nickpettican/81850826a82f36c0eda8 to your computer and use it in GitHub Desktop.
sort some Galaxy-downloaded BED format
names(galaxy_bed) <- c("chrom",
"chromStart",
"chromEnd",
"name",
"score",
"strand",
"blockCount",
"blockSizes")
# This shows how to 'subset' with either the $ operator or [[
eg_with_square_brackets <- galaxy_bed[["chromStart"]]
eg_with_dollar <- galaxy_bed$chromStart
# The most hassle free way to sort a data frame is to use a package from the community's "tidy data" set of packages,
# like dplyr, tidyr etc.
library(plyr) #load the plyr package, which wraps the order function from base R
# preview the top of the sorted table
head(arrange(galaxy_bed, decreasing=FALSE))
# observe how the decreasing parameter is passed to order by the ellipsis in plyr's arrange function
# see: http://ipub.com/r-three-dots-ellipsis/ etc for description of what the ellipsis does in an R function
head(arrange(galaxy_bed, galaxy_bed$name, decreasing = FALSE))
head(arrange(galaxy_bed, galaxy_bed$name, decreasing = TRUE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment