【发布时间】:2021-06-04 20:33:56
【问题描述】:
我想知道在 R 中清理调查数据时是否有人可以解决以下问题。
假设一项调查有 Q1“你的性别是什么”:男性、女性、不想说。调查中没有人选择“Prefer not to say”,所以我跑频率的时候只看到:
Q1 男:8,女:8。
有没有办法将“不想说”中的代码写入 Q1,这样当我运行频率时,我会看到:
Q1 Male : 8, Female: 8, Prefer not to say: 0.
这是一些示例数据和代码:
dat_in<-read_table2("ID Gender
1 1
2 1
3 1
4 1
5 1
6 2
7 2
8 2
9 2
10 2
11 2
12 2
13 1
14 2
15 1
16 2
")
data_cat <- dat_in %>% mutate_if(is.numeric,as.character) %>% mutate(across(matches("Gender"), ~fct_recode(., "Female" = "1","Male"="2")))
lapply(select_if(data_cat, is.factor),
function(x) {
df = data.frame(table(x))
return(df)
})
【问题讨论】: