df.sorter: Sort a 'data.frame' by Rows or Columns

Description Usage Arguments Details Note Author(s) Examples

View source: R/df.sorter.R

Description

The df.sorter function allows you to sort a data.frame by columns or rows or both. You can also quickly subset data columns by using the var.order argument.

Usage

1
df.sorter(data, var.order = names(data), col.sort = NULL, at.start = TRUE)

Arguments

data

The source data.frame.

var.order

The new order in which you want the variables to appear. See Details

col.sort

The columns within which there is data that need to be sorted. See Details.

at.start

Should the pattern matching be from the start of the variable name? Defaults to TRUE.

Details

var.order

col.sort

Note

If you are sorting both by variables and within the columns and using numeric indexes as opposed to variable names, the col.sort order should be based on the location of the columns in the new data.frame, not the original data.frame.

Author(s)

Ananda Mahto

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Make up some data
set.seed(1)
dat = data.frame(id = rep(1:5, each=3), times = rep(1:3, 5),
            measure1 = rnorm(15), score1 = sample(300, 15),
            code1 = replicate(15, paste(sample(LETTERS[1:5], 3),
                                        sep="", collapse="")),
            measure2 = rnorm(15), score2 = sample(150:300, 15),
            code2 = replicate(15, paste(sample(LETTERS[1:5], 3),
                                        sep="", collapse="")))
# Preview your data
dat

# Change the variable order, grouping related columns
# Note that you do not need to specify full variable names,
#    just enough that the variables can be uniquely identified
head(df.sorter(dat, var.order = c("id", "ti", "cod", "mea", "sco")))

# As above, but sorted by 'times' and then 'id'
head(df.sorter(dat,
          var.order = c("id", "tim", "cod", "mea", "sco"),
          col.sort = c(2, 1)))

# Drop 'measure1' and 'measure2', sort by 'times', and 'score1'
head(df.sorter(dat,
          var.order = c("id", "tim", "sco", "cod"),
          col.sort = c(2, 3)))

# Just sort by columns, first by 'times' then by 'id'
head(df.sorter(dat, col.sort = c("times", "id")))

# Pattern matching anywhere in the variable name
head(df.sorter(dat, var.order= "co", at.start=FALSE))

mrdwab/mrdwabmisc documentation built on May 23, 2019, 7:15 a.m.