作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想更改所有性别条目“男、女、女、男、男等”。为了更加一致,所以它只有 3 个元素(男性、女性和非二元)。这是我当前的代码
# Cleaning of Specific Variable Types
removed <- removed %>%
mutate(gender=substr(toupper(gender), 1, 1))
removed <- removed %>%
mutate(gender=case_when(
gender == "M"~"Male",
gender == "F"~"Female",
gender == "N"~"Non-binary")
)
最佳答案
这可能是长版本,但它应该可以工作:数据来自 jay.sf(非常感谢)
性别
中的唯一条目str_detect
和模式应用 case_when
条件:# Capitalize each value to avoid interaction of "man" and "woman" in str_detect
# check for unique elements in `gender`
removed$gender <- str_to_title(removed$gender)
unique(removed$gender)
[1] "Male" "Woman" "Other" "Mtf" "Female"
[6] "Man" "Ftm" "Androgyne"
# define pattern for each category
Male <- paste(c("Male", "Man"), collapse = "|")
Female <- paste(c("Woman", "Female"), collapse = "|")
Non_binary <- paste(c("Other", "Mtf", "Ftm", "Androgyne"), collapse= "|")
# apply category with `case_when` and pattern:
library(dplyr)
library(stringr)
removed %>%
mutate(gender = case_when(
str_detect(gender, Male) ~ "Male",
str_detect(gender, Female) ~ "Female",
str_detect(gender, Non_binary) ~ "Non-binary"))
输出:
gender
1 Male
2 Female
3 Male
4 Non-binary
5 Non-binary
6 Female
7 Male
8 Non-binary
9 Male
10 Male
11 Male
12 Female
13 Non-binary
14 Female
15 Female
16 Non-binary
17 Male
18 Female
19 Non-binary
20 Non-binary
21 Non-binary
22 Female
23 Female
24 Female
25 Female
26 Male
27 Non-binary
28 Male
29 Female
30 Non-binary
关于r - 我如何将所有不同的条目分类为男性、女性和非二元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69240277/
我是一名优秀的程序员,十分优秀!