taxlist (version 0.1.7)

replace_x: Data Manipulation

Description

Functions provided for fast replacement and update of data.

Usage

replace_x(x, old, new)

replace_idx(x, idx1, idx2, new)

replace_na(x, idx1, idx2, new)

insert_rows(x, y)

Arguments

x

A vector to be modified or a data frame in the case of insert_rows.

old,new

Vectors containing the values to be replaced and the updated values, respectively.

idx1,idx2

Indices applied for the values in 'x' and the values to be replaced, respectively.

y

Data frame including rows to be inserted in 'x'.

Value

A vector or data frame with the modified values.

Details

These are functions implemented for efficient coding of insert and replace routines.

The functions replace_x and replace_idx replace values in vectors, in the first case comparing values in the vector and in the second one by using indices. The function replace_na works in the same way as replace_idx, but carries out the replacement only if the previous value is a NA.

The function insert_rows inserts y as new rows in x. If y contains columns absent in x, they will be added to the output data frame.

Examples

Run this code
# NOT RUN {
library(taxlist)

## Replace values in vector
replace_x(letters, c("b", "p", "f"), c("bee", "pork", "fungus"))

## Replace values using indices
replace_idx(letters, 1:length(letters), c(2,7,17), c("second","seven",
				"seventeenth"))

## Replace values if they are NAs
letters[2] <- NA
replace_na(letters, 1:length(letters), c(1:3), c("alpha", "beta", "zeta"))

## Merge data frames including new columns
data(iris)
iris$Species <- paste(iris$Species)
new_iris <- data.frame(Species=rep("humilis", 2), Height=c(15,20), stringsAsFactors=FALSE)
insert_rows(iris, new_iris)
# }

Run the code above in your browser using DataCamp Workspace