R语言 如何将3列中的信息合并为一列而不复制它们

wgx48brx  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(204)

我需要有一个年龄列。目前我有三个是under30age30to64age65plus。我需要把所有这些合并成一个单一的列。我也想做同样的activeactive1列。我想做到这一点,而不复制或遗漏的基本数据。

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"))
vawmfj5a

vawmfj5a1#

使用dplyr::case_when()

library(dplyr)

dat <- dat %>%
  mutate(
    ageCat = case_when(
      under30 == "under 30" ~ "under 30",
      age30to64 == "age 30 to 64" ~ "30 to 64",
      age65plus == "65 plus" ~ "65 plus"
    ),
    ageCat = factor(ageCat, c("under 30", "30 to 64", "65 plus"))
  )

结果:

#> dat %>%
#+   select(under30, age30to64, age65plus, ageCat)

# A tibble: 5 × 4
  under30 age30to64           age65plus ageCat  
  <fct>   <fct>               <fct>     <fct>   
1 30 plus age 30 to 64        under 65  30 to 64
2 30 plus age 30 to 64        under 65  30 to 64
3 30 plus age 30 to 64        under 65  30 to 64
4 30 plus under 30 or 65 plus 65 plus   65 plus 
5 30 plus age 30 to 64        under 65  30 to 64

相关问题