![](/img/trans.png)
[英]Replace column values with NA based on a different column or row position with tidyverse
[英]How to recode values in R tidyverse based on the values in a different column
我有兩個分類變量,時間點(0,1)和最高資格(1:8)。
最初,當我重新編碼時,我使用了以下代碼,效果很好:
df2<- df%>%
mutate(highest_qualification = recode(highest_qualification,
"1" = "No formal qualifications",
"2" = "Certificate / diploma",
"3" = "School certificate",
"4" = "University degree",
"5" = "Higher school certificate",
"6" = "Higher university degree",
"7" = "Trade / apprenticeship",
"8" = "Prefer not to say"))
但是,我注意到僅對於基線數據(時間點 == 0)“4”或“大學學位”和“7”或“貿易/學徒”是錯誤的,應該交換。
后續數據(時間點 ==1)並非如此。
我嘗試了以下代碼,但它不起作用:
df2<- df%>%
mutate(highest_qualification = case_when(
timepoint == "0" & highest_qualification ==
"1" ~ "No formal qualifications",
"2" ~ "Certificate / diploma",
"3" ~ "School certificate",
"4" ~ "Trade / apprenticeship",
"5" ~ "Higher school certificate",
"6" ~ "Higher university degree",
"7" ~ "University degree",
"8" ~ "Prefer not to say",
timepoint == "1" & highest_qualification ==
"1" ~ "No formal qualifications",
"2" ~ "Certificate / diploma",
"3" ~ "School certificate",
"4" ~ "University degree",
"5" ~ "Higher school certificate",
"6" ~ "Higher university degree",
"7" ~ "Trade / apprenticeship",
"8" ~ "Prefer not to say"))
基本上我試圖在基線數據中切換“貿易/學徒”和“大學學位”(時間點==0)
有沒有人有一些提示或解決方法。
先感謝您。
我建議在 if_else 中重新編碼,例如:
mtcars %>%
select(vs, am) %>%
mutate(am2 = if_else(vs == 0, # timepoint == "0" in your example
recode(am,
"1" = "01",
"0" = "00"),
recode(am,
"1" = "11",
"0" = "10")))
結果
vs am am2
Mazda RX4 0 1 01
Mazda RX4 Wag 0 1 01
Datsun 710 1 1 11
Hornet 4 Drive 1 0 10
Hornet Sportabout 0 0 00
Valiant 1 0 10
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.