【问题标题】:RStudio wrongfully shows bracket warningsRStudio 错误地显示括号警告
【发布时间】:2020-08-05 04:01:27
【问题描述】:

我在 ideone.com (https://ideone.com/Z2pVQp ) 因为只有在您保存文件后,RStudio 才会在第 1、29、34、87 行旁边错误地显示不匹配的刹车警告标志。

由于该函数非常大,我将其全部发布在问题部分,但会给你例如第 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)
    )

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

【问题讨论】:

  • 你能解释一下为什么或帮助我让它做它应该做的事情吗?
  • 可能我错了。它可能有效,但你可以试试这个替代方案missinggames %&gt;% mutate(points_team_home = case_when(Goals_team_home &gt; Goals_team_away ~3, Goals_team_home == Goals_team_away ~ 1, TRUE ~ 0), points_team_away = case_when(Goals_team_home &gt; Goals_team_away ~0, Goals_team_home == Goals_team_away ~ 1, TRUE ~ 3)) 吗?
  • 非常好。我在脑海中寻找那个答案,但想不出。这也解决了刹车问题。

标签: r rstudio


【解决方案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))

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 1970-01-01
    • 2012-01-24
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2018-12-06
    • 2021-10-12
    相关资源
    最近更新 更多