Alexander
Alexander

Reputation: 4635

rep function strange error

When I perform:

a <- seq(1,1.5,0.1)
b <- c(1,1.1,1.4,1.5)

x <- rep(c(a,b),times=c(2,1))

Error in rep(c(a, b), c(2, 1)) : invalid 'times' argument

Why?

Upvotes: 2

Views: 372

Answers (2)

Andre Elrico
Andre Elrico

Reputation: 11490

Marked answer is already perfect: Here an alternative using mapply

unlist(mapply(function(x,n)rep(x,n),list(a,b),c(2,1)))

Upvotes: 1

akrun
akrun

Reputation: 887391

When we concatenate (c) two vectors, it becomes a single vector. If the idea would be to replicate 'a' by 2 and 'b' by 1, we place them in a list, and use rep. The output will be a list, which can be unlisted to get a vector.

 unlist(rep(list(a,b), c(2,1)))

Upvotes: 7

Related Questions