Search code examples
rfor-looprep

Including text and variable in rep() for loop


Having some trouble trying to assign classifiers to my data. I have 500 rows of data, and I'd like to assign "City 1", "City 2", etc... up to "City 50". And repeat each one 10 times. So it should have "City 1" 10 times, and then "City 2" 10 times, until "City 50" 10 times. Where I'm struggling is having text in quotes as well as a variable call in the same argument of rep().

for (j in 1:50)
{
City<-rep(paste("City",j),1,each=10)
}

My output for this is just simply a list of "City 50" 10 times. I'm not quite sure why this is happening.


Solution

  • As rep and paste are both vectorized, this can be done without any loop

    rep(paste("City", 1:50), each = 10)