【发布时间】:2018-05-20 15:12:38
【问题描述】:
我在 Occupation 列中有两个 NA 值的数据,我正在尝试使用 dplyr 将这些值替换为单词 Pensioner。
这就是我所拥有的。
data <- data %>%
filter(is.na(Occupation) & Yrs_Empleo <= -999 & Organisation == "XNA" & Income_type == "Pensioner")
我已经尝试过mutate_at 和replace_na 以及一些ifelse 语句,但我似乎无法弄清楚如何正确地做到这一点。
所以基本上我试图根据三个条件替换列Occupation 中的所有NA 值,然后一旦满足这三个条件,就用工作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")
【问题讨论】: