vasili111
vasili111

Reputation: 6930

Add to every number in scalar/vector `1` and insert the result after that number

I have many numerical scalars/vectors, like:

a <- 1
b <- c(2,4)
c <- c(5,6,7)
d <- c(60, 556, 30, 4, 5556, 111232)

Now I need to add to every number in scalar/vector 1 and insert the result after that number. The solution should work with any numerical scalars and vectors. So result should look like:

a <- c(1,2)
b <- c(2,3,4,5)
c <- c(5,6,6,7,7,8)
d <- c(60, 61, 556, 557, 30, 31, 4, 5, 5556, 5557, 111232, 111233)

How this can be done?

Upvotes: 0

Views: 220

Answers (1)

IceCreamToucan
IceCreamToucan

Reputation: 28675

lst <- list(
  a = 1,
  b = c(2,4),
  c = c(5,6,7),
  d = c(60, 556, 30, 4, 5556, 111232))

lapply(lst, function(x) as.vector(rbind(x, x + 1)))
# $`a`
# [1] 1 2
# 
# $b
# [1] 2 3 4 5
# 
# $c
# [1] 5 6 6 7 7 8
# 
# $d
#  [1]     60     61    556    557     30     31      4      5   5556   5557 111232 111233

This is pretty much a dupe of this, but not exactly so I'll let someone else make the call.

Upvotes: 2

Related Questions