我需要有一个年龄列。目前我有三个是under30
,age30to64
和age65plus
。我需要把所有这些合并成一个单一的列。我也想做同样的active
和active1
列。我想做到这一点,而不复制或遗漏的基本数据。
structure(list(fruits = c(0, 0, 0, 0, 1), veggies = c(0, 1, 1,
1, 1), age = structure(c(7L, 8L, 9L, 10L, 6L), levels = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"
), class = "factor"), under30 = structure(c(1L, 1L, 1L, 1L, 1L
), levels = c("30 plus", "under 30"), class = "factor"), age30to64 = structure(c(2L,
2L, 2L, 1L, 2L), levels = c("under 30 or 65 plus", "age 30 to 64"
), class = "factor"), age65plus = structure(c(1L, 1L, 1L, 2L,
1L), levels = c("under 65", "65 plus"), class = "factor"), arthritis = structure(c(1L,
2L, 1L, 1L, 1L), levels = c("No arthritis", "Arthritis"), class = "factor"),
gender = structure(c(2L, 2L, 2L, 1L, 2L), levels = c("male",
"female"), class = "factor"), genhealth = structure(c(3L,
3L, 2L, 3L, 2L), levels = c("Excellent", "Very good", "Good",
"Fair", "Poor"), class = "factor"), education = structure(c(5L,
6L, 4L, 6L, 6L), levels = c("1", "2", "3", "4", "5", "6"), class = "factor"),
income = structure(c(8L, 8L, 7L, 6L, 8L), levels = c("1",
"2", "3", "4", "5", "6", "7", "8"), class = "factor"), active = structure(c(2L,
1L, 2L, 1L, 2L), levels = c("Not active", "Active"), class = "factor"),
active1 = structure(c(2L, 1L, 2L, 1L, 3L), levels = c("Low",
"Moderate", "Vigorous"), class = "factor"), bmi = c(18.2199993133545,
27.4599990844727, 21.9699993133545, 35.939998626709, 39.8600006103516
), bmicat = structure(c(1L, 3L, 2L, 4L, 4L), levels = c("Underweight",
"Normal", "Overweight", "Obese"), class = "factor"), activetimes = c(20,
0, 5, 0, 8), ageCat = structure(c(2L, 2L, 2L, 3L, 2L), levels = c("under30",
"age30to64", "over64"), class = "factor")), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
1条答案
按热度按时间vawmfj5a1#
使用
dplyr::case_when()
:结果: