我有以下data.frame
Name<-c("Jack","Jerry","Emma","Andy","Jayde","Lynn","Liam")
Education<-c("Master","Master","Master","Bach","Bach","PhD","PhD")
Salary<-c(20000,20000,20000,30000,10000,70000,70000)
People<-data.frame(Name,Education,Salary)
我现在必须使用for循环(愚蠢,我知道)循环框架,找到&#34;教育&#34;等级,并增加工资增长。
怎么办呢?
答案 0 :(得分:0)
即使按照评论中提到的那样做得更好,你也可以用丑陋的方式做到:
for (i in 1:nrow(People)) {
if (People$Education[i] == "Bach") {
People$Salary[i] <- People$Salary[i]+1000
} else if (People$Education[i] == "Master") {
People$Salary[i] <- People$Salary[i]+2000
} else {
People$Salary[i] <- People$Salary[i]+3000
}
}