如何将3列年龄数据及2列活动数据合并为单列且无重复遗漏?
解决方案
1. 合并年龄列(under30、age30to64、age65plus)
先看这三列的取值逻辑:
- under30 标记是否为30岁以下
- age30to64 标记是否为30-64岁
- age65plus 标记是否为65岁以上
用case_when就能精准匹配出每个样本的年龄分组,不会遗漏或重复:
library(dplyr) df <- df %>% mutate( combined_age = case_when( under30 == "under 30" ~ "under 30", age30to64 == "age 30 to 64" ~ "age 30 to 64", age65plus == "65 plus" ~ "65 plus", TRUE ~ NA_character_ # 兜底处理异常情况,确保没有漏网数据 ) %>% factor(levels = c("under 30", "age 30 to 64", "65 plus")) )
验证下:原数据里第4行的ageCat是over64,对应age65plus是"65 plus",合并后combined_age会是"65 plus",其他行都是"age 30 to 64",和原分类完全一致。
2. 合并活动列(active、active1)
active列管的是“是否活跃”,active1列是“活跃程度”——看数据就能发现,active为"Not active"时,active1都是"Low";active为"Active"时,active1会给出具体强度(Moderate/Vigorous)。
你可以选择两种合并方式:
方式一:保留完整状态描述
把“是否活跃”和“强度”结合,信息更直观:
df <- df %>% mutate( combined_active = case_when( active == "Not active" ~ "Not active", active == "Active" & active1 == "Low" ~ "Active (Low)", active == "Active" & active1 == "Moderate" ~ "Active (Moderate)", active == "Active" & active1 == "Vigorous" ~ "Active (Vigorous)", TRUE ~ NA_character_ ) %>% factor() )
方式二:简化分类
因为Not active已经对应Low,直接简化成:不活跃就标"Not active",活跃就用active1的强度值:
df <- df %>% mutate( combined_active = case_when( active == "Not active" ~ "Not active", TRUE ~ as.character(active1) ) %>% factor(levels = c("Not active", "Low", "Moderate", "Vigorous")) )
完整代码
把两步整合到一起,还可以删掉没用的原列:
library(dplyr) # 加载你的数据 df <- 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")) # 合并+清理数据 df_clean <- df %>% # 合并年龄列 mutate( combined_age = case_when( under30 == "under 30" ~ "under 30", age30to64 == "age 30 to 64" ~ "age 30 to 64", age65plus == "65 plus" ~ "65 plus", TRUE ~ NA_character_ ) %>% factor(levels = c("under 30", "age 30 to 64", "65 plus")) ) %>% # 合并活动列(用简化方式) mutate( combined_active = case_when( active == "Not active" ~ "Not active", TRUE ~ as.character(active1) ) %>% factor(levels = c("Not active", "Low", "Moderate", "Vigorous")) ) %>% # 删掉冗余的原列 select(-under30, -age30to64, -age65plus, -active, -active1) # 查看处理后的结果 print(df_clean)
结果说明
combined_age完美对应三个年龄分组,和原数据的ageCat逻辑一致,没有重复或遗漏。combined_active整合了两列的关键信息:既保留了“不活跃”的状态,又区分了活跃时的不同强度,所有有效数据都没丢。
内容的提问来源于stack exchange,提问作者Costa Wesele
相关产品推荐
相关产品推荐

