2

I have created a vector via the following and would like to repeat the value of each column for 307 obs so that I can cbind it to a dataframe I have:

weightvalues <- c(0.3,0.7,
         0.3,0.4,0.2,0.1,
         0.85,0.15,
         0.25,0.25,0.2,0.2,0.1,
         0.4,0.6,
         0.4,0.6)
names(weightvalues) <- c("IC.TurnoverWeight","Branch.TurnoverWeight",
                         "Total.CallsWeight","Substantiated.CallsWeight","Unsubstantiated.CallsWeight","Branch.HeadcountWeight",
                         "PC.Cancel.RateWeight","TC.Cancel.RateWeight",
                         "VOE.2017.SERVWeight","VOE.2017.Acct.EmpWeight","VOE.2017.CustWeight","VOE.2017.EthicsWeight","VOE.2017.SafetyWeight",
                         "June.VOE.Participation.PercentWeight","July.VOE.Participation.PercentWeight",
                         "NPS.Score.TCWeight","NPS.Score.PCWeight")
weightvalues
FinalLaborUnrestDatLVs <- cbind(FinalLaborUnrestDatLVs,weightvalues)

Obviously, since my data frame "FinalLaborUnrestDatLVs" has 307 observations it won't bind with my vector of only one. Is there an easy solution here?

Edit: I guess I can use rep, I'm just struggling figuring out this function.

J Walt
  • 141
  • 1
  • 9
  • Including a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in your question will increase your chances of getting an answer. – Samuel Nov 10 '17 at 17:55

1 Answers1

0

You can transpose the vector to make it a matrix of one row and use apply to rep every column 307 times:

FinalLaborUnrestDatLVs = cbind(FinalLaborUnrestDatLVs, apply(t(weightvalues), 2, rep, 307))
acylam
  • 18,231
  • 5
  • 36
  • 45