Flash Sale | 50% off

Last chance! 50% off unlimited learning

Sale ends in


fasterRaster (version 8.4.0.5)

dropRows,data.table-method: Remove rows in a data.table, data.frame, or matrix.

Description

As of September of 2023, the data.table package does not have a function for removing rows by index. This function does this job.

Usage

# S4 method for data.table
dropRows(x, drops)

# S4 method for data.frame dropRows(x, drops)

# S4 method for matrix dropRows(x, drops)

Value

A data.table or data.frame.

Arguments

x

A data.table or data.frame.

drops

Numeric, integer, or logical vector: Indices or indicators of rows to remove.

If a logical vector is supplied, rows that correspond to TRUE will be removed. If the vector is shorter than the number of rows, values of drops will be recycled.

Examples

Run this code

library(data.table)

dt <- data.table(
   x = 1:10,
   y = letters[1:10],
   z = rnorm(10)
)

# make some values NA
dt[x == 4 | x == 8, y := NA_character_]
dt

# Replace NAs:
replaceNAs(dt, replace = -99, cols = "y")
dt

# Drop rows:
dropped <- dropRows(dt, 8:10)
dropped

# NB May not print... in that case, use:
print(dropped)

# We can also use replaceNAs() on vectors:
y <- 1:10
y[c(2, 10)] <- NA
replaceNAs(y, -99)

# Same as:
y <- 1:10
y[c(2, 10)] <- NA
y[is.na(y)] <- -99

Run the code above in your browser using DataLab