Insert Elements in a Vector in R

Insert elements into a vector at given indexes

You can do some magic with indexes:

First create vector with output values:

probs <- rep(TRUE, 15)
ind <- c(5, 10)
val <- c( probs, rep(FALSE,length(ind)) )
# > val
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
# [13] TRUE TRUE TRUE FALSE FALSE

Now trick. Each old element gets rank, each new element gets half-rank

id  <- c( seq_along(probs), ind+0.5 )
# > id
# [1] 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0
# [16] 5.5 10.5

Then use order to sort in proper order:

val[order(id)]
# [1] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
# [13] TRUE TRUE TRUE TRUE TRUE

insert elements in a vector in R

Try this:

result <- vector("list",5)
result[c(TRUE,FALSE)] <- split(a, cumsum(seq_along(a) %in% (c(3,7)+1)))
result[c(FALSE,TRUE)] <- list(b,d)
f <- unlist(result)

identical(f, e)
#[1] TRUE

EDIT: generalization to arbitrary number of insertions is straightforward:

insert.at <- function(a, pos, ...){
dots <- list(...)
stopifnot(length(dots)==length(pos))
result <- vector("list",2*length(pos)+1)
result[c(TRUE,FALSE)] <- split(a, cumsum(seq_along(a) %in% (pos+1)))
result[c(FALSE,TRUE)] <- dots
unlist(result)
}


> insert.at(a, c(3,7), b, d)
[1] 2 3 4 2 1 9 10 2 4 0 1 19

> insert.at(1:10, c(4,7,9), 11, 12, 13)
[1] 1 2 3 4 11 5 6 7 12 8 9 13 10

> insert.at(1:10, c(4,7,9), 11, 12)
Error: length(dots) == length(pos) is not TRUE

Note the bonus error checking if the number of positions and insertions do not match.

Insert elements into a vector at a given position, a given number of times

We can split the 'x' into a list of vector based on a grouping index created with %/%``, concatenate (c) with the 'y' using 'Map and unlist the list to get the vector

unlist(Map(c, split(x, (x-1)%/%10), list(y)), use.names = FALSE)
#[1] 1 2 3 4 5 6 7 8 9 10 1 2 11 12 13 14 15 16 17 18 19 20 1 2 21
#[26] 22 23 24 25 26 27 28 29 30 1 2 31 32 33 34 35 36 37 38 39 40 1 2 41 42
#[51] 43 44 45 46 47 48 49 50 1 2

How do I insert elements systematically within a vector in R?

You could use rbind as follows:

y <- seq(from=2, to=10, by=2)*-1
y
# [1] -2 -4 -6 -8 -10
as.vector(rbind(0, y))
# [1] 0 -2 0 -4 0 -6 0 -8 0 -10

R: insert elements into vector (a variation)

One option would be

v1 <- numeric(length(x)+length(ind))
v1[setdiff(seq_along(v1), ind)] <- x
v1
#[1] 1 2 3 0 0 0 4 5 6 0 0 7

data

x <- 1:7
ind <- c(4:6,10:11)

How to insert a new element to a vector?

its easy (like etienne posted)
if you want a vector with same length as a result (like in your example) you can use length().

x<-rnorm(10)
x<-c(NA,x)[1:length(x)]

inserting specific elements into vector using R

Create a NA vector of the same length as 'a' and then replace based on the non NA elements in 'a'

b <- replace(rep(NA, length(a)), !is.na(a), b)

-output

b
#[1] 1 2 4 3 6 NA 5 7 NA 8 40 NA

Or more compactly, do the replace on 'a'

replace(a, !is.na(a), b)
[1] 1 2 4 3 6 NA 5 7 NA 8 40 NA


Related Topics



Leave a reply



Submit