首页 > 解决方案 > 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

标签: rif-statementdplyrtidyverse

解决方案


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

推荐阅读