0

Right now, this working loop is pasting the supervisor's scores for a given year into new columns (supervisor.score1:supervisor.score4) on the appropriate employee row. It achieves this by looking at an employee row i and finding the employee row(s) for the supervisor and year listed in row i. Then it takes the first row on that list of matching rows, and pastes the scores (score1:score 4) from that row into supervisor.score1:supervisor.score4 for the corresponding employee row i.

employeeID year supervisor score1:score 4 supervisor.score1:supervisor.score4

for (i in (1:nrow(data))){
  matchvector <- which(data[,1] == data[i,3] & data[,2] == data[i,2])
  if (length(matchvector) > 0) {
    case <- matchvector[1]
    data[i, namevector] <- data[case, supervisor.score1:supervisor.score4]}
  if (length(matchvector[1]) < 1){
    data[i, supervisor.score1:supervisor.score4] <- NA}
}  

Is there a way to convert this loop into a function that can be called with apply?

nusbaume
  • 27
  • 1
  • 1
  • 6
  • Please share a few lines of data using `dput()` to turn this into a [reproducible example](http://stackoverflow.com/q/5963269/903061). – Gregor Thomas Nov 17 '16 at 19:31
  • Also please be a little clearer - what are you hoping to `apply` this over? A list of data frames? Something else? What needs to be parameterized in the function (i.e., what is different in each application)? Or do you want to replace the `for` loop with `apply`? – Gregor Thomas Nov 17 '16 at 19:33

0 Answers0