これを使用してください。あなたがしたことに何も問題はありませんが、それは長い方法です。
install.packages("dplyr")
library(dplyr)
a <- cbind.data.frame(status=c("Never married, cohabiting","Never married, not cohabiting","Married, spouse present",
"Married, spouse absent","Separated, cohabiting","Separated, not cohabiting",
"Divorced, cohabiting","Divorced, not cohabiting","Widowed, cohabiting",
"Widowed, not cohabiting"), value=c(1084 ,2441,2744,188,18,66,202,361,4,12))
a=a %>%
mutate(married_status=as.numeric(status %in%
c("Married, spouse present",
"Married, spouse absent","Separated, cohabiting","Separated, not cohabiting",
"Divorced, cohabiting","Divorced, not cohabiting","Widowed, cohabiting",
"Widowed, not cohabiting"))) %>%
select(-status)
質問があれば教えてください。
質問をしているときに[再現可能な例]を入力してください。また、期待される出力の例を明記してください。 –