Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

replace_consecutive

Replaces consecutive elements


Description

If x[i+1]=x[i] then x[i+1] is replaced by by for i=1,...length(x)-1.

Usage

replace_consecutive(x, by = "", fun_for_identical = base::identical)

Arguments

x

A character or factor.

by

A character with length 1.

fun_for_identical

A function with two arguments called x and y.

Details

The = is defined by function identical by default. This function can be changed by argument fun_for_identical

Value

A character, same length as x, now with consecutives replaced by by. If length(x) < 2, x is returned unchanged.

Examples

x <- rep(c('a','b','c','d'), times=c(2,4,1,3))
x
## Not run: replace_consecutive(x)
# NA should not be identical. So change fun_for_identical
fun_for_identical <- function(x,y) !is.na(x) && !is.na(y) && identical(x,y)
x <- c(1,1,3,3,NA,NA, 4)
x
## Not run: replace_consecutive(x, by="99")
## Not run: replace_consecutive(x, by="99", fun_for_identical = fun_for_identical)

atable

Create Tables for Reporting Clinical Trials

v0.1.10
GPL-3
Authors
Armin Ströbel [aut, cre] (<https://orcid.org/0000-0002-6873-5332>), Alan Haynes [aut] (<https://orcid.org/0000-0003-1374-081X>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.