如果在R中满足条件,则用多列中的NA替换值 [英] replace values with NA across multiple columns if a condition is met in R

查看:62
本文介绍了如果在R中满足条件,则用多列中的NA替换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果条件满足,我试图用多个列中的NA替换各个值中的值.

I'm trying to replace values across values with NA across multiple columns if a condition is met.

这是一个示例数据集:

library(tidyverse)
sample <- tibble(id = 1:6,
                 team_score = 5:10,
                 cent_dept_test_agg = c(1, 2, 3, 4, 5, 6),
                 cent_dept_blue_agg = c(15:20),
                 num_in_dept = c(1, 1, 2, 5, 100, 6))

当num_in_dept为1时,我希望包含cent_dept _.* _ agg的列为NA,所以它看起来像这样:

I want the columns that contain cent_dept_.*_agg to be NA when num_in_dept is 1, so it looks like this:

library(tidyverse)
solution <- tibble(id = 1:6,
                   team_score = 5:10,
                   cent_dept_test_agg = c(NA, NA, 3, 4, 5, 6),
                   cent_dept_blue_agg = c(NA, NA, 17:20),
                   num_in_dept = c(1, 1, 2, 5, 100, 6))

我已经尝试使用replace_with_na_at(来自 nanier 软件包)和na_if(来自 dplyr 软件包),但是我无法弄清楚.我知道我的选择标准是正确的(dplyr :: matches("cent_dept _.* _ agg"),但我不知道解决方案.

I've tried using replace_with_na_at (from the nanier package) and na_if (from the dplyr package), but I can't figure it out. I know my selection criteria is correct (dplyr::matches("cent_dept_.*_agg"), but I can't figure out the solution.

在我的实际数据集中,我有很多以cent_dept开头并以agg结尾的列,因此与组件匹配的选择用户非常重要.

In my actual dataset, I have many columns that start with cent_dept and end with agg, so it's very important that the selection users that matches component.

谢谢您的帮助!

推荐答案

我们可以使用 mutate_at 选择与匹配'cent_dept'和 replace "num_in_dept"为1的值

We can use mutate_at to select the columns that matches 'cent_dept' and replace the values where 'num_in_dept' is 1

library(dplyr)
sample %>%
    mutate_at(vars(matches('^cent_dept_.*_agg$')), ~ 
                  replace(., num_in_dept == 1, NA))
# A tibble: 6 x 5
#     id team_score cent_dept_test_agg cent_dept_blue_agg num_in_dept
#  <int>      <int>              <dbl>              <int>       <dbl>
#1     1          5                 NA                 NA           1
#2     2          6                 NA                 NA           1
#3     3          7                  3                 17           2
#4     4          8                  4                 18           5
#5     5          9                  5                 19         100
#6     6         10                  6                 20           6


base R 中,我们也可以这样做

nm1 <- grep('^cent_dept_.*_agg$', names(sample))
sample[nm1] <- lapply(sample[nm1], function(x) 
         replace(x, sample$num_in_dept == 1, NA))

或者可以通过

sample[nm1] <-  sample[nm1] * NA^(sample$num_in_dept == 1)

这篇关于如果在R中满足条件,则用多列中的NA替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆