sections: Create sections

Description Usage Arguments Details Value See Also Examples

View source: R/character.R

Description

The ‘logical’ method treats a logical vector by regarding TRUE as indicating separation. It creates a factor that could be used with split to split the logical vector, or any equal-length object from which it was created, into according groups. The ‘character’ method splits a character vector according to a pattern or to a given output partial string length.

Usage

1
2
3
4
5
6
7
8
  sections(x, ...)

  ## S3 method for class 'logical'
 sections(x, include = TRUE, ...)

  ## S3 method for class 'character'
 sections(x, pattern, invert = FALSE,
    include = TRUE, perl = TRUE, ...)

Arguments

x

Logical vector. It is an error if NA values are contained.

include

Logical scalar indicating whether or not the separator positions should also be included in the factor levels instead of being coded as NA. If include is NA, the behaviour is special; pairs of runs of distinct values are expected (length(x) must be an even number), each pair yields a distinct factor level, and the output does not contain any NA values.

pattern

Scalar. If of mode ‘character’, passed to grepl from the base package. If numeric, used to indicate the lengths of the partial strings to extract.

invert

Negate the results of grepl?

perl

Logical scalar passed to grepl.

...

Optional arguments passed to and from other methods, or between the methods.

Details

When applying split, positions corresponding to NA factor levels will usually be removed. Thus note the action of the include argument, and note that the positions of TRUE values that are followed by other TRUE values are always set to NA, irrespective of include. The ‘character’ method using a pattern works by passing this pattern to grepl, the result to the ‘logical’ method and this in turn to split.

Value

The ‘logical’ method returns an ordered factor, its length being the one of x. The levels correspond to a groups whose indexes correspond to the index of a TRUE value in x plus the indexes of the FALSE values immediately following it. ‘logical’ method returns a list of character vectors.

See Also

base::split base::grepl

Other character-functions: clean_filenames, map_filenames, map_files,

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
## 'logical' method

# clean input
x <- c(TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE)
(y <- sections(x))
stopifnot(identical(as.ordered(c(1, 1, 1, 2, 2, 3, 3)), y))

# now exclude the separators
y <- sections(x, include = FALSE)
stopifnot(identical(as.ordered(c(NA, 1, 1, NA, 2, NA, 3)), y))

# and now put pairs of runs together
y <- sections(x, include = NA)
stopifnot(identical(as.ordered(c(1, 1, 1, 2, 2, 3, 3)), y))

# leading FALSE
x <- c(FALSE, x)
(y <- sections(x))
stopifnot(identical(as.ordered(c(1, 2, 2, 2, 3, 3, 4, 4)), y))
(y <- try(sections(x, include = NA), silent = TRUE))
stopifnot(inherits(y, "try-error")) # no pairs throughout => error

# adjacent TRUEs and trailing TRUE
x <- c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE)
(y <- sections(x))
stopifnot(identical(as.ordered(c(1, NA, 2, 2, 3, 3, NA, 4, 4, 5)), y))
(y <- sections(x, include = NA))
stopifnot(identical(as.ordered(c(1, 1, 1, 2, 2, 3, 3, 3, 4, 4)), y))

# several adjacent TRUEs
x <- c(FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
(y <- sections(x))
stopifnot(identical(as.ordered(c(1, NA, NA, NA, 2, 2, 3, 3)), y))
(y <- try(sections(x, include = NA), silent = TRUE))
stopifnot(inherits(y, "try-error")) # no pairs throughout => error

## 'character' method

# using a specified length
x <- c("abcdef", "ghijk")
(y <- sections(x, 2))
stopifnot(is.list(y), length(y) == 2)
stopifnot(y[[1]] == c("ab", "cd", "ef"), y[[2]] == c("gh", "ij", "k"))

# using a regexp pattern
x <- c(">abc", ">def", "acgtagg", ">hij", "gatattag", "aggtagga") # FASTA
(y <- sections(x, "^>", include = TRUE))
stopifnot(identical(y, list(`1` = x[2:3], `2` = x[4:6])))
(y <- sections(x, "^>", include = FALSE))
stopifnot(identical(y, list(`1` = x[3], `2` = x[5:6])))

pkgutils documentation built on May 2, 2019, 5:49 p.m.

Related to sections in pkgutils...