Populate column based on another column in R

Viewed 38

I am trying to figure out how to populate the NA cells in the user-type column based on the user column. Would appreciate any help, thank you.

user user_type
123 emp
124 man
125 man
124 NA
123 NA
125 NA
1 Answers

We could combine group_by and fill:

library(dplyr)
library(tidyr)

df %>% 
  group_by(user) %>% 
  fill(user_type, .direction = "down")
   user user_type
  <int> <chr>    
1   123 emp      
2   124 man      
3   125 man      
4   124 man      
5   123 emp      
6   125 man 
Related