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?