RStudio错误地显示括号警告

问题描述 投票:0回答:1

我在ideone.com上检查了我的功能(https:/ideone.comZ2pVQp) 因為 RStudio 在您儲存檔案後,才會在第 1、29、34、87 行旁邊錯誤地顯示不匹配的 braket 警告標誌。

由於這個函數相當大,我不打算把它全部貼在問題部分,但會給您舉例說明第 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)
    )

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

r rstudio
1个回答
1
投票

我们可以使用 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))
© www.soinside.com 2019 - 2024. All rights reserved.