[英]Creating a single variable with multiple categories in R
I am working with a survey that asks questions based on the answer to previous questions. 我正在进行一项调查,根据之前问题的答案提出问题。 I need to create a single variable with multiple categories. 我需要创建一个包含多个类别的单个变量。
An example: 一个例子:
(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?
I need to create one variable that is 'total value bank account', with multiple categories: 我需要创建一个“总价值银行帐户”变量,其中包含多个类别:
Yes <5_value1
Yes <5_value2
Yes <5_value3
Yes <5_value4
Yes <5_value5
Yes >5_total_value
No
How can I do this in R? 我怎么能在R中这样做?
Thank you. 谢谢。
Sample Data 样本数据
You did not tell us how your raw data looks like, so I assume it is in a data frame as follows: 您没有告诉我们您的原始数据是什么样的,所以我假设它在数据框中如下:
(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
Code 码
Then you can use ifelse
to create a new variable: 然后你可以使用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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.