r - if_else is true, make NA for range of columns
问题描述
here is a test DF
test_df <- structure(list(plant_id = c("A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O"),
value = c(100, 105, 120, 61, 52,
30, 21, 301, 200, 92,
1, 0, 81, 202, 42),
C1 = c(2, 5, 3, 1, 5,
1, 21, 4, 2, 3,
1, 0, 8, 55, 2),
C2 = c(9, 7, 3, 1, 52,
30, 21, 1, 8, 2,
1, 0, 1, 2, 4),
C3 = c(100, 3, 120, 2, 1,
3, 1, 301, 2, 3,
31, 30, 2, 11, 24)),
row.names = c(NA, -15L), class = "data.frame",
.Names = c("plant_sp", "value", "C1", "C2", "C3"))
I want to build an if_else condition which checks if the value under column "value" is less than 100. If it is, I want it to make range of column as NA (for example column at postion 3-4 would make column C1 and C2 as NA). Pipe solution would be amazing.
Thanks a lot, Ido
解决方案
Are you looking for this:
library(dplyr)
test_df %>% mutate(across(C1:C2, ~ if_else(value < 100, NA_real_, .)))
plant_sp value C1 C2 C3
1 A 100 2 9 100
2 B 105 5 7 3
3 C 120 3 3 120
4 D 61 NA NA 2
5 E 52 NA NA 1
6 F 30 NA NA 3
7 G 21 NA NA 1
8 H 301 4 1 301
9 I 200 2 8 2
10 J 92 NA NA 3
11 K 1 NA NA 31
12 L 0 NA NA 30
13 M 81 NA NA 2
14 N 202 55 2 11
15 O 42 NA NA 24
推荐阅读
- javascript - 松露测试中的时间延迟
- python - Gmail API ID 不工作,但线程 ID 工作
- ruby-on-rails - config.assets.compile = false - 使 heroku 不在生产中显示我的图像
- c# - 为什么我的身份数据库表没有迁移到 Azure?
- c - 从输入文件中读取字符串/单词和整数
- ruby-on-rails - 将默认日期设置为“所有日期”到 datepicker
- postgresql - 什么 PL/pgSQL 异常与 PostgreSQL 中 Oracle 的 VALUE_ERROR 等效?
- node.js - 通过发送 200 阻止 Express-API 并继续执行?
- maven - 验证代码更改后自述文件中的命令行示例是否正常工作
- spring-security-oauth2 - Spring OAuth2 - 令牌端点的基本身份验证标头中用户名的 URL 编码