如何更新dplyr管道中的值?

Wil*_*car 4 r dplyr

我想更新新列中的值.

这是我的数据:

people<- c("father", "parents", "father", "children", "girl", "boy", "grand father", "grand mother", "grandparents" ) 
dataset0 <- data.frame(people)
dataset0
Run Code Online (Sandbox Code Playgroud)

并输出:

father              
parents             
father              
children                
girl                
boy             
grand father                
grand mother                
grandparents
Run Code Online (Sandbox Code Playgroud)

预期产量:

 people           people_update

father            parents   
parents           parents   
father            parents   
children          children
girl              children
boy               children
grand father      grandparents          
grand mother      grandparents      
grandparents      grandparents
Run Code Online (Sandbox Code Playgroud)

我试着用 replace()

dataset <- dataset0 %>%
   mutate(people_update = replace(people, people =="girl", "children")) %>%
   mutate(people_update = replace(people, people =="boy", "children")) 
 dataset
Run Code Online (Sandbox Code Playgroud)

但这不起作用.第二个mutate()命令取消第一个mutate()命令.

www*_*www 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"
  ))
Run Code Online (Sandbox Code Playgroud)


Sot*_*tos 6

这可以通过嵌套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
Run Code Online (Sandbox Code Playgroud)


Uwe*_*Uwe 5

替代case_when()或嵌套if_else()是与转换表连接map:

library(dplyr)
dataset0 %>% left_join(map)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

在哪里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
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)

如果只有少数选定的项目需要翻译,则可以修改代码:

# 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"      
)
Run Code Online (Sandbox Code Playgroud)

请注意,"mother"已添加到转换表中.

dataset0 %>% 
  left_join(map2) %>% 
  # copy unchanged items
  mutate(people_update = if_else(is.na(people_update), people, people_update))
Run Code Online (Sandbox Code Playgroud)
        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
Run Code Online (Sandbox Code Playgroud)