first.data.frame: 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 a data.frame. Depending on margin these the first n rows or columnns.

Usage

1
2
3
## S3 method for class 'data.frame'
first(x, n = 1, keep = FALSE, margin = c(1, 2),
  drop = FALSE, strict = c(0, 1, 2, 3))

Arguments

x

data.frame. The object from which to select elements.

n

numeric. The number of elements to select from the beginning. See argument margin.

keep

logical. TRUE: keep elements n + 1 through NROW(x) as attribute keep.

margin

numeric. 1 stands for rows, 2 stand for columns.

drop

logical. See argument drop in data.frame.

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

A data.frame or a vecor depending on drop.

Author(s)

Janko Thyson janko.thyson@gmail.com

References

http://github.com/rappster/selectr

See Also

first, first.default

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.