dplyr基于多个条件替换列中的na值

dplyr基于多个条件替换列中的na值

本文介绍了dplyr基于多个条件替换列中的na值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在职业列中具有两个NA值的数据,并且尝试使用 dplyr 进行替换带有 Pensioner 字的值。

I have this data with two NA values in the Occupation column and I am trying to use dplyr to replace the values with the word Pensioner.

这就是我所拥有的。

data <- data %>%
  filter(is.na(Occupation) & Yrs_Empleo <= -999 & Organisation == "XNA" & Income_type == "Pensioner")

我尝试了 mutate_at replace_na 和一些 ifelse 语句,但我似乎无法弄清楚如何正确

I have tried mutate_at and replace_na and some ifelse statements but I just cannot seem to figure out how to correctly do it.

所以基本上我想替换列职业中的所有 NA 根据三个条件,然后在满足这三个条件后,将其替换为已退休的工作。

So basically I am trying to replace all NA values in column Occupation based on three conditions and then once those three conditions have been met, replace with the work retired.

structure(list(Yrs_Empleo = c(1.74520547945205, 3.25479452054795,
0.616438356164384, 8.32602739726027, 8.32328767123288, 4.35068493150685,
8.57534246575342, 1.23013698630137, -1000.66575342466, 5.53150684931507,
1.86027397260274, -1000.66575342466, 7.44383561643836), Occupation = c("Laborers",
"Core staff", "Laborers", "Laborers", "Core staff", "Laborers",
"Accountants", "Managers", NA, "Laborers", "Core staff", NA,
"Laborers"), Organisation = c("Business Entity Type 3", "School",
"Government", "Business Entity Type 3", "Religion", "Other",
"Business Entity Type 3", "Other", "XNA", "Electricity", "Medicine",
"XNA", "Business Entity Type 2"), Income_type = c("Working",
"State servant", "Working", "Working", "Working", "State servant",
"Commercial associate", "State servant", "Pensioner", "Working",
"Working", "Pensioner", "Working")), .Names = c("Yrs_Empleo",
"Occupation", "Organisation", "Income_type"), row.names = c(NA,
13L), class = "data.frame")


推荐答案

您可以像这样使用 case_when

data %>%
  mutate(Occupation = case_when(is.na(Occupation) & Yrs_Empleo <= -999 & Organisation == "XNA" & Income_type == "Pensioner" ~ "retired",
                                TRUE ~ Occupation))

      Yrs_Empleo  Occupation           Organisation          Income_type
1      1.7452055    Laborers Business Entity Type 3              Working
2      3.2547945  Core staff                 School        State servant
3      0.6164384    Laborers             Government              Working
4      8.3260274    Laborers Business Entity Type 3              Working
5      8.3232877  Core staff               Religion              Working
6      4.3506849    Laborers                  Other        State servant
7      8.5753425 Accountants Business Entity Type 3 Commercial associate
8      1.2301370    Managers                  Other        State servant
9  -1000.6657534     retired                    XNA            Pensioner
10     5.5315068    Laborers            Electricity              Working
11     1.8602740  Core staff               Medicine              Working
12 -1000.6657534     retired                    XNA            Pensioner
13     7.4438356    Laborers Business Entity Type 2              Working

这篇关于dplyr基于多个条件替换列中的na值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:09