r - 如何在不使用 R 中的公式的情况下改变列(部分)
问题描述
我想用向量替换NA
列上的那些。我的结果不正常,而且我认为这不是做我想做的事情的正确方法。y
subst <- c(5,6,7)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- tibble(x = c("a", "a", "b", "b", "b", "c", "c"),
y = c(2, 3, NA, NA, NA, 1, 2))
subst <- c(5, 6, 7)
df2 <- df %>% mutate(y = ifelse(x == "b", subst, y))
# But I want to obtain
df3 <- tibble(x = c("a", "a", "b", "b", "b", "c", "c"),
y = c(2, 3, 5, 6, 7, 1, 2))
Created on 2021-06-07 by the reprex package (v2.0.0)
解决方案
推荐阅读
- php - URL 重写 htaccess。从 id 更改为 alias
- javascript - 尝试在 nodejs 上显示主页时 JSON 输入意外结束
- python - PySpark 在 Dataframe 列中插入一个常量 SparseVector
- github - 如何在 Cargo.toml 的依赖项中指定某个提交?
- c - 使用 strtok 分组假标记拆分字符串
- r - 测试 Limma 没有变化
- xl-deploy - XLdeploy:无法创建具有正确名称的文件夹
- xml - 动态改变按钮的动作
- java - 事务不工作(Manager tx 问题)
- java - 更新应用程序后如何删除数据