first.default: Select first element(s)

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/first.r

Description

Selects the first n elements of an object. The default method of the convenience wrapper first.

Usage

1
2
## Default S3 method:
first(x, n = 1, keep = FALSE, strict = c(0, 1, 2, 3))

Arguments

x

ANY-class. The object from which to select elements.

n

numeric. The number of elements to select from the beginning.

keep

logical. TRUE: keep elements n + 1 through NROW(x) as attribute keep. FALSE: just return the selected elements.

strict

numeric.

  • 0 : disregard invalid values for n without signaling a condition and return with an object as if the maximum valid n had been specified

  • 1 : signal a message for invalid values for n and return with an object as if the maximum valid n had been specified

  • 2 : signal a warning for invalid values for n and return with an object as if the maximum valid n had been specified

  • 3 : signal an error (see stop) for invalid values for n

Value

Subset of x. Exact value depends on the class of x.

Author(s)

Janko Thyson janko.thyson@gmail.com

References

http://github.com/rappster/selectr

See Also

first, first.data.frame

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
51
52
53
54
55
56
57
58
59
60
##------------------------------------------------------------------------------
## Vector //
##------------------------------------------------------------------------------

x <- letters
first(x)
first(x, 3)
first(x, 3, keep = TRUE)

## Conditions //
first(x, 30)
first(x, 30, strict = 1)
first(x, 30, strict = 2)
try(first(x, 30, strict = 3))

##------------------------------------------------------------------------------
## List //
##------------------------------------------------------------------------------

x <- as.list(letters)
first(x)
first(x, 3)
res <- first(x, 3, keep = TRUE)
attributes(res)

## Conditions //
first(x, 30)
first(x, 30, strict = 1)
first(x, 30, strict = 2)
try(first(x, 30, strict = 3))

##------------------------------------------------------------------------------
## Data frame //
##------------------------------------------------------------------------------

x <- data.frame(a= 1:5, b = letters[1:5], stringsAsFactors = FALSE)

## Rows:
first(x)
first(x, 3)
first(x, 3, keep = TRUE)

## Columns:
first(x, margin = 2)
first(x, 2, margin = 2)
first(x, 3, margin = 2)

first(x, margin = 2, drop = TRUE)
first(x, 2, margin = 2, drop = TRUE)

## Conditions:
first(x, 10)
first(x, 10, strict = 1)
first(x, 10, strict = 2)
try(first(x, 10, strict = 3))

first(x, 3, margin = 2)
first(x, 3, margin = 2, strict = 1)
first(x, 3, margin = 2, strict = 2)
try(first(x, 3, margin = 2, strict = 3))

rappster/selectr documentation built on May 26, 2019, 11:57 p.m.