我正在与一项调查进行合作,该调查基于对先前问题的回答来提出问题。我需要创建一个具有多个类别的变量。
一个例子:
(1) Do you have a bank account? Yes/No
(2) If yes: How many bank accounts do you have, <5 or >5?
(3) If >5: what is the total value? If <5, what is the value of account 1 thru 5?
我需要创建一个具有多个类别的变量,即“总价值银行帐户”:
Yes <5_value1
Yes <5_value2
Yes <5_value3
Yes <5_value4
Yes <5_value5
Yes >5_total_value
No
如何在R中做到这一点?
谢谢。
答案 0 :(得分:0)
样本数据
您没有告诉我们原始数据的样子,所以我认为它位于数据框中,如下所示:
(my_data <- data.frame(id = rep(1:3, c(3, 1, 1)),
has.bank.account = rep(c("yes", "no"), c(4, 1)),
nr.of.accounts = rep(c(3, 6, NA), c(3, 1, 1)),
amount = c(1000 * 1:3, 10000, NA)))
# id has.bank.account nr.of.accounts amount
# 1 1 yes 3 1000
# 2 1 yes 3 2000
# 3 1 yes 3 3000
# 4 2 yes 6 10000
# 5 3 no NA NA
代码
然后,您可以使用ifelse
创建一个新变量:
with(my_data,
ifelse(has.bank.account == "no",
"no",
paste0("Yes ",
ifelse(nr.of.accounts > 5, ">5_", "<=5_"),
amount
)
)
)
# [1] "Yes <=5_1000" "Yes <=5_2000" "Yes <=5_3000" "Yes >5_10000" "no"