grapes-rows-grapes: Operator to Subset Rows While Preserving Attributes

%rows%R Documentation

Operator to Subset Rows While Preserving Attributes

Description

The binary operator %rows% returns the subset of specified rows for a data frame without removing column attributes.

Usage

x %rows% y

Arguments

x

A data frame

y

An integer vector, logical vector, or character vector specifying the rows in x to keep.

Value

A data frame.

Examples

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 )

# Normal indexing removes attributes
str( dtf[1:2,] )

# Can use operator to avoid losing attributes
str( dtf %rows% 1:2 )


rettopnivek/arfpam documentation built on Oct. 20, 2024, 7:24 p.m.