使用for,if和else if函数的新变量

时间:2018-07-15 08:08:30

标签: r for-loop if-statement

我创建了一个包含三个不同测试成绩的数据框。我现在想在同一数据帧(“ highest_score”)中创建一个新变量,说明三个得分中哪个得分最高。我创建了以下代码来做到这一点:

test <- data.frame(test_1, test_2, test_3) 
# this gives me a dataframe with scores from three different tests (6 observations in each) 

然后

for (i in 1:nrow(test)) {
  if ((test$test_1[i] > test$test_2[i]) & (test$test_1[i] > test$test_3[i]))
      test$highest_score <- "Test 1 is the individual's highest score!"
} else if ((test$test_2[i] > test$test_1[i]) & (test$test_2[i] > 
test$test_3[i]))
       test$highest_score <- "Test 2 is the individual's highest score!"
} else if ((test$test_3[i] > test$test_1[i]) & (test$test_3[i] > 
test$test_2[i]))
       test$highest_score <- "Test 3 is the individual's highest score!"
} else 
  NULL
}
}

当我运行代码时,新变量'highest_score'打印出'Test 3是个人的最高分!”对于所有观察结果,即使事实并非如此。

如果有人能够让我知道我要去哪里,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

由于您没有示例测试数据,因此我创建了一个。您要寻找的功能是max.col。如果分数不符,请阅读帮助(?max.col)。我将所有内容包装在一个简单的函数中,没有错误处理即可返回您想要的文本。

# create reproducible example 6 long as in OP's question.
set.seed(1234)
test <- data.frame(test_1 = sample(1:10, 6), test_2 = sample(1:10, 6), test_3 = sample(1:10, 6)) 

test
  test_1 test_2 test_3
1      2      1      3
2      6      3      9
3      5      6     10
4      8      4      6
5      9      5      2
6      4      9      7

#example of which column has the maximum value
max.col(test)
[1] 3 3 3 1 1 2

# wrap everything in function
my_func <- function(data){
  #which column is has the maximum value
  wm <- max.col(data)
  out <- ifelse(wm == 1, "Test 1 is the individual's highest score!", 
                ifelse(wm == 2, "Test 2 is the individual's highest score!",
                       "Test 3 is the individual's highest score!"))
  return(out)
}
my_func(test)
[1] "Test 3 is the individual's highest score!" "Test 3 is the individual's highest score!" "Test 3 is the individual's highest score!"
[4] "Test 1 is the individual's highest score!" "Test 1 is the individual's highest score!" "Test 2 is the individual's highest score!"

将其添加到测试数据中。

test$highest_score <- my_func(test)