r - 在“if”子句中使用复合语句
问题描述
我发现了一个涉及模板的类似问题,超出了我的问题范围。我希望能够说类似的话:if (a and b) then (do something)。这是一个例子:
t1 <- tribble(
~state, ~county,
"New York", "Bronx",
"New York", "Richmond",
"New York", "Albany",
"Virginia", "Richmond"
)
five_boroughs = c("Bronx", "Kings", "New York", "Queens", "Richmond")
if t1$state == "New York" && t1$county in five_boroughs
t1$county = "New York City"
使用 &、&&、in 或 %in% 将纽约市置于弗吉尼亚州。我为打电话给县区的纽约人道歉。
解决方案
我们可以用case_when
library(dplyr)
library(stringr)
t1 %>%
mutate(county = case_when(state == 'New York' &
county %in% five_boroughs~ str_c(state, ' City')))
推荐阅读
- javascript - 如果关闭其他设置,则不会保存 Wordpress 插件设置
- pandas - 将 Pandas 数据帧 UPSERT 插入雪花数据库
- python - AWS WAF CDK Python 如何更改规则操作
- shell - Ansible 使用 shell 模块,我如何 ssh 并保持在同一目录中?
- swiftui - 如何在 swiftui 中为整个应用程序重用单个列表视图以显示数据并导航回具有不同数据源的相应父级
- reactjs - 使用 Yup 的 react-hook-form 解析器类型错误
- python - 如何在 Python 中保存图像矩阵并在 MATLAB 中使用?
- php - 在 php 中调用 Oracle 函数和触发器需要帮助
- c# - 如何在 C# 中的 MongoDB UpdateOne 上实现字符串小于过滤器
- flutter - Flutter:如何在平面内对角旋转矩形以进行绘画