r - 使用 mutate 和 case_when 时从现有列中插入值
问题描述
当满足 x 列中的条件时,我想向具有相应“y”值的数据框添加一z
列。ds
library(tidyverse)
ds <- tibble(x = 1:5,y = 6:10)
ds%>%
mutate(
z = case_when(
x == 3 ~ y,
TRUE ~ NA_character_
)
)
我希望y列中的相应值在z
列中,但是如何以正确的方式引用y列?
解决方案
这是你想要的?
ds%>%
mutate(
z = case_when(
x == 3 ~ y,
TRUE ~ NA_integer_
)
)
# A tibble: 5 x 3
x y z
<int> <int> <int>
1 1 6 NA
2 2 7 NA
3 3 8 8
4 4 9 NA
5 5 10 NA
推荐阅读
- c# - System.InvalidOperationException 实例失败
- c# - dotnet 4.0 和 registerprovider
- http - 如何在 Flutter GET Api 请求中添加多个标头
- ruby-on-rails - 如何获取上一年启用但当年未启用的数据
- python - 如何在python3每5分钟重新启动一次我的代码
- arrays - 在脚本中访问数组中的元素
- spring-boot - 在 JQPL 查询中执行更新/删除查询
- ios - 具有泛型的 Swift func where 约束是一个自符合的协议
- typescript - 从子项目的 tsconfig.json 中排除某些类型
- javascript - JavaScript setInterval() 方法没有像我期望的那样工作