r-如果_else为true,则为列的范围设置NA



这是一个测试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"))

我想建立一个if_else条件,该条件检查列";值";小于100。如果是,我希望它将列的范围设为NA(例如,3-4位的列将使C1和C2列为NA(。管道解决方案将是惊人的。

非常感谢,Ido

你在找这个吗:

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

基本R选项-

cols <- c('C1', 'C2')
test_df[test_df$value < 100, cols] <- NA
test_df
#   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

使用case_when

library(dplyr)
test_df %>%
mutate(across(C1:C2, ~ case_when(value >=100 ~ value))

相关内容

  • 没有找到相关文章

最新更新