gpt4 book ai didi

r - dplyr 根据多个条件替换列中的 na 值

转载 作者:行者123 更新时间:2023-12-04 11:58:48 29 4
gpt4 key购买 nike

我在 Occupation 列中有两个 NA 值的数据,我正在尝试使用 dplyr 用单词 Pensioner 替换这些值。

这就是我所拥有的。

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

我已经尝试过 mutate_atreplace_na 以及一些 ifelse 语句,但我似乎无法弄清楚如何正确执行它。

所以基本上我试图根据三个条件替换 NA 列中的所有 Occupation 值,然后一旦满足这三个条件,就用工作 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

关于r - dplyr 根据多个条件替换列中的 na 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50436248/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com