natka_m
natka_m

Reputation: 1632

How to append the same item to a vector many times?

I'm rather new to R, and I've been looking for a solution but can't find an appropriate function. I need a vector of characters in the form:

v <- c("a", "a", "b", "b", "b", "b", "b", ...)

where "b" is repeated 65 times. I know I could do a for loop:

v <- c("a", "a")
for (i in 1:65) {v <- c(v, "b")}

but the loop doesn't seem very neat to me. In Python, I'd simply do:

v = ['a', 'a'] + ['b'] * 65

Is there any way of creating such a vector in R, or am I just trying too hard to write Pythonic code in R?

Upvotes: 2

Views: 1409

Answers (1)

mischva11
mischva11

Reputation: 2956

You can repeat with the repeat rep() function and concatenate/append with c()

so c("a,","a",rep("b",65)) would do this for you

Upvotes: 3

Related Questions