我想更新新列中的值。
这是我的数据:
people<- c("father", "parents", "father", "children", "girl", "boy", "grand father", "grand mother", "grandparents" )
dataset0 <- data.frame(people)
dataset0
输出:
father
parents
father
children
girl
boy
grand father
grand mother
grandparents
预期产出:
people people_update
father parents
parents parents
father parents
children children
girl children
boy children
grand father grandparents
grand mother grandparents
grandparents grandparents
我尝试使用replace()
dataset <- dataset0 %>%
mutate(people_update = replace(people, people =="girl", "children")) %>%
mutate(people_update = replace(people, people =="boy", "children"))
dataset
但这不起作用。第二个mutate()
命令取消第一个mutate()
命令。
答案 0 :(得分:8)
尝试case_when
指定多个替换。它比多个ifelse
语句更简洁。
library(dplyr)
dataset <- dataset0 %>%
mutate(people_update = case_when(
people %in% c("father", "parents") ~ "parents",
people %in% c("children", "girl", "boy") ~ "children",
people %in% c("grandparents", "grand father", "grand mother") ~ "grandparents",
TRUE ~ "NA"
))
答案 1 :(得分:5)
case_when()
或嵌套if_else()
的替代方法是加入翻译表map
:
library(dplyr)
dataset0 %>% left_join(map)
Joining, by = "people" people people_update 1 father parents 2 parents parents 3 father parents 4 children children 5 girl children 6 boy children 7 grand father grandparents 8 grand mother grandparents 9 grandparents grandparents Warning message: Column `people` joining factor and character vector, coercing into character vector
其中map
由
map <- tribble(
~people, ~people_update,
"father", "parents",
"parents", "parents",
"children", "children",
"girl", "children",
"boy", "children",
"grand father", "grandparents",
"grand mother", "grandparents",
"grandparents", "grandparents"
)
map
# A tibble: 8 x 2 people people_update <chr> <chr> 1 father parents 2 parents parents 3 children children 4 girl children 5 boy children 6 grand father grandparents 7 grand mother grandparents 8 grandparents grandparents
如果只有少数选定的项目需要翻译,则可以修改代码:
# define only items to be changed
map2 <- tribble(
~people, ~people_update,
"father", "parents",
"mother", "parents",
"girl", "children",
"boy", "children",
"grand father", "grandparents",
"grand mother", "grandparents"
)
请注意,"mother"
已添加到转换表中。
dataset0 %>%
left_join(map2) %>%
# copy unchanged items
mutate(people_update = if_else(is.na(people_update), people, people_update))
people people_update 1 father parents 2 parents parents 3 father parents 4 children children 5 girl children 6 boy children 7 grand father grandparents 8 grand mother grandparents 9 grandparents grandparents
答案 2 :(得分:3)
这可以通过嵌套的ifelse
语句来处理,即
library(dplyr)
dataset0 %>%
mutate(v1 = ifelse(people %in% c('father', 'mother', 'parents'), 'parents',
ifelse(people %in% c('girl', 'boy', 'children'), 'children', 'grandparents')))
# people v1
#1 father parents
#2 parents parents
#3 father parents
#4 children children
#5 girl children
#6 boy children
#7 grand father grandparents
#8 grand mother grandparents
#9 grandparents grandparents
答案 3 :(得分:2)
重点在于,在第二个mutate中,您在x参数中继续使用people
而不是people_update
。
dataset <- dataset0 %>%
mutate(people_update = replace(people, people =="girl", "children")) %>%
mutate(people_update = replace(people_update, people =="boy", "children"))
dataset