Как насчет без всякой петли
rep("math",sum(df$course == "math"))
# returns
[1] "math" "math" "math"
с
df <- structure(list(ID = c(10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L),
sem = c(1, 1, 1, 2, 2, 2, 1, 2), course = c("math", "phys",
"other", "math", "phys2", "chem", "other", "math")), class = "data.frame", row.names = c(NA,
-8L))