I was wondering if anyone could help me concencate two vectors of strings:
For example: hello and hi, that repeat 130 times in a dataframe.
When in the dataframe column I would like for the order to be hello, (130 times) followed by hi (also 130 times), then hello (130 more times), then hi (130 times) again. So they should appear 4 times total (2 times each in order)
This is what I tried so far but it does not seem to work
hello <- c(rep( "hello", 130))
hi<- c(rep( "hi", 130))
style <- c(hello, hi, hello, hi)
CodePudding user response:
The general solution to string concatenation in R is the paste
function. In this case, you just paste the same vectors multiple times:
hello <- rep("hello", 130)
hi <- rep("hi", 130)
result <- paste(hello, hi, hello, hi)
print(result)
There are other ways to handle this as well, e.g. using sprintf
. I suggest consulting ?paste
for details on usage.
CodePudding user response:
I think you need rep
with each
:
df <- data.frame(my_col = rep(rep(c("hello", "hi"), each=130),2))