group_by-ranges: Group a Ranges by one or more variables

Description Usage Arguments Details Value Accessors Examples

Description

The function group_by takes a Ranges object and defines groups by one or more variables. Operations are then performed on the Ranges by their "group". ungroup() removes grouping.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## S3 method for class 'GenomicRanges'
group_by(.data, ..., add = FALSE)

## S3 method for class 'GroupedGenomicRanges'
ungroup(x, ...)

## S3 method for class 'GroupedGenomicRanges'
groups(x)

## S3 method for class 'GroupedIntegerRanges'
groups(x)

Arguments

.data

a Ranges object.

...

Variable names to group by. These can be either metadata columns or the core variables of a Ranges.

add

if .data is already a GroupedRanges object, when add = FALSE the (default), group_by() will override existing groups. If add = TRUE, additional groups will be added.

x

a GroupedRanges object.

Details

group_by() creates a new object of class GroupedGenomicRanges if the input is a GRanges object or an object of class GroupedIntegerRanges if the input is a IRanges object. Both of these classes contain a slot called groups corresponding to the names of grouping variables. They also inherit from their parent classes, Ranges and GenomicRanges respectively. ungroup() removes the grouping and will return either a GRanges or IRanges object.

Value

The group_by() function will return a GroupedRanges object. These have the same appearance as a regular Ranges object but with an additional groups slot.

Accessors

To return grouping variables on a grouped Ranges use either

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
set.seed(100)
df <- data.frame(start = 1:10,
                 width = 5,
                 gc = runif(10),
                 cat = sample(letters[1:2], 10, replace = TRUE))
rng <- as_iranges(df)
rng_by_cat <- rng %>% group_by(cat)
# grouping does not change appearance or shape of Ranges
rng_by_cat
# a list of symbols
groups(rng_by_cat)
# ungroup removes any grouping
ungroup(rng_by_cat)
# group_by works best with other verbs
grng <- as_granges(df,
                   seqnames = "chr1",
                   strand = sample(c("+", "-"), size = 10, replace = TRUE))

grng_by_strand <- grng %>% group_by(strand)
grng_by_strand
# grouping with other verbs
grng_by_strand %>% summarise(gc = mean(gc))
grng_by_strand %>% filter(gc == min(gc))
grng_by_strand %>%
  ungroup() %>%
  summarise(gc = mean(gc))

plyranges documentation built on Nov. 8, 2020, 7:36 p.m.