RStudio 错误地显示括号警告

RStudio wrongfully shows bracket warnings

我在 ideone.com 上检查了我的功能(https://ideone.com/Z2pVQp ) 因为 RStudio 仅在您保存文件后错误地在第 1、29、34、87 行旁边显示不匹配的 braket 警告标志。

由于函数非常大,所以我会在问题部分 post 全部介绍,但会给你例如第 29 - 34 行,这可能是解决这个问题的关键。

   missinggames <-  map_df(1:nrow(missinggames), ~if(missinggames$Goals_team_home[.x] > missinggames$Goals_team_away[.x])
      mutate(missinggames[.x,], points_team_home =  3, points_team_away = 0) else if
      (missinggames$Goals_team_home[.x] == missinggames$Goals_team_away[.x])
        mutate(missinggames[.x,], points_team_home =  1, points_team_away = 1) else
          mutate(missinggames[.x,], points_team_home =  0, points_team_away = 3)
    )

我是否遗漏了什么或者我该如何解决这个问题?

我们可以在这里使用 case_when :

library(dplyr)

missinggames %>%
  mutate(points_team_home = case_when(Goals_team_home > Goals_team_away ~3, 
                                      Goals_team_home == Goals_team_away ~ 1, 
                                      TRUE ~ 0), 
         points_team_away = case_when(Goals_team_home > Goals_team_away ~0, 
                                      Goals_team_home == Goals_team_away ~ 1, 
                                      TRUE ~ 3))