Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

r - dplyr replacing na values in a column based on multiple conditions

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.

This is what I have.

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

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.

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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can use if_else

data %>%
  mutate(Occupation = if_else(is.na(Occupation) & 
                         Yrs_Empleo <= -999 &
                         Organisation == "XNA", "Pensioner", 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   Pensioner                    XNA            Pensioner
#10     5.5315068    Laborers            Electricity              Working
#11     1.8602740  Core staff               Medicine              Working
#12 -1000.6657534   Pensioner                    XNA            Pensioner
#13     7.4438356    Laborers Business Entity Type 2              Working

Or use replace

data %>% 
   mutate(Occupation = replace(Occupation, 
             is.na(Occupation) & 
                         Yrs_Empleo <= -999 &
                         Organisation == "XNA",
               "Pensioner"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...