grapes-rows-grapes.Rd
The binary operator %rows% returns the subset of specified rows for a data frame without removing column attributes.
x %rows% y
A data frame
An integer vector, logical vector, or character vector specifying the rows in x to keep.
A data frame.
dtf <- data.frame(
X1 = 1:4,
X2 = LETTERS[1:4],
X3 = c( TRUE, TRUE, FALSE, FALSE )
)
attributes( dtf$X1 ) <- list( Example_attr = "Integer" )
attributes( dtf$X2 ) <- list( Example_attr = "Character" )
attributes( dtf$X3 ) <- list( Example_attr = "Logical" )
# Each column has an attribute
str( dtf )
#> 'data.frame': 4 obs. of 3 variables:
#> $ X1: int 1 2 3 4
#> ..- attr(*, "Example_attr")= chr "Integer"
#> $ X2: chr "A" "B" "C" "D"
#> ..- attr(*, "Example_attr")= chr "Character"
#> $ X3: logi TRUE TRUE FALSE FALSE
#> ..- attr(*, "Example_attr")= chr "Logical"
# Normal indexing removes attributes
str( dtf[1:2,] )
#> 'data.frame': 2 obs. of 3 variables:
#> $ X1: int 1 2
#> $ X2: chr "A" "B"
#> $ X3: logi TRUE TRUE
# Can use operator to avoid losing attributes
str( dtf %rows% 1:2 )
#> 'data.frame': 2 obs. of 3 variables:
#> $ X1: int 1 2
#> ..- attr(*, "Example_attr")= chr "Integer"
#> $ X2: chr "A" "B"
#> ..- attr(*, "Example_attr")= chr "Character"
#> $ X3: logi TRUE TRUE
#> ..- attr(*, "Example_attr")= chr "Logical"