如何使用R中的cross函数改变多列中的值?

vql8enpb  于 2022-12-25  发布在  其他
关注(0)|答案(3)|浏览(112)

我有一个 Dataframe ,我想遍历以_qc结尾的所有列,如果值为“4”,则将NA设置为不带_qc后缀的相应列。
例如,如果是名为chla_adjusted_qc == 4的列的值,则将chla_adjusted的值设置为NA。

library(tidyverse)

df <- tibble(
  chla_adjusted = c(100, 2),
  chla_adjusted_qc = c("4", "1"),
  bbp_adjusted = c(0.1, 9999),
  bbp_adjusted_qc = c("2", "4")
)

df
#> # A tibble: 2 × 4
#>   chla_adjusted chla_adjusted_qc bbp_adjusted bbp_adjusted_qc
#>           <dbl> <chr>                   <dbl> <chr>          
#> 1           100 4                         0.1 2              
#> 2             2 1                      9999   4

所需输出为

tibble(
  chla_adjusted = c(NA, 2),
  chla_adjusted_qc = c("4", "1"),
  bbp_adjusted = c(0.1, NA),
  bbp_adjusted_qc = c("2", "4")
)
#> # A tibble: 2 × 4
#>   chla_adjusted chla_adjusted_qc bbp_adjusted bbp_adjusted_qc
#>           <dbl> <chr>                   <dbl> <chr>          
#> 1            NA 4                         0.1 2              
#> 2             2 1                        NA   4

到目前为止,我所做的是获取当前列名并找到要设置NA值的相应列。

df |>
  mutate(across(ends_with("_qc"), \(var) {
    # If var is chla_adjusted_qc, then lets modify the value in chla_adjusted
    col <- str_remove(cur_column(), "_qc")

    # if (var == "4") {
    #   # What to do here?
    # }
  }))
#> # A tibble: 2 × 4
#>   chla_adjusted chla_adjusted_qc bbp_adjusted bbp_adjusted_qc
#>           <dbl> <chr>                   <dbl> <chr>          
#> 1           100 chla_adjusted             0.1 bbp_adjusted   
#> 2             2 chla_adjusted          9999   bbp_adjusted

谢谢你。
创建于2022年12月20日,使用reprex v2.0.2

xcitsw88

xcitsw881#

df %>%
  mutate(across(ends_with("_qc"),
                ~ replace(cur_data()[[ sub("_qc$", "", cur_column()) ]], . == 4L, NA),
                .names = "{sub('_qc$', '', .col)}"))
# # A tibble: 2 × 4
#   chla_adjusted chla_adjusted_qc bbp_adjusted bbp_adjusted_qc
#           <dbl> <chr>                   <dbl> <chr>          
# 1            NA 4                         0.1 2              
# 2             2 1                        NA   4
jvlzgdj9

jvlzgdj92#

碱R溶液:

for(v in grep("_qc$",names(df), value=TRUE)){
  df[[sub("_qc$","",v)]][df[[v]]==4] <- NA
}

> df
# A tibble: 2 × 4
  chla_adjusted chla_adjusted_qc bbp_adjusted bbp_adjusted_qc
          <dbl> <chr>                   <dbl> <chr>          
1            NA 4                         0.1 2              
2             2 1                        NA   4              
>
wz1wpwve

wz1wpwve3#

我们可以从dplyover使用across2

library(dplyover)
df %>% 
   mutate(across2(ends_with('adjusted'), ends_with('_qc'), 
    ~ case_when(.y !=4 ~ .x ), .names = "{xcol}"))
  • 输出
# A tibble: 2 × 4
  chla_adjusted chla_adjusted_qc bbp_adjusted bbp_adjusted_qc
          <dbl> <chr>                   <dbl> <chr>          
1            NA 4                         0.1 2              
2             2 1                        NA   4

相关问题