Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

Basically I need to fill in missing data in a column.

Let's say, for instance, I am having the below data frame and in that each employee particularly works in 1 of the locations and does not work in more than one. So in the below illustration since the non-missing values for Bill show East, then Bill works in the East location. My question is how do I substitute all the NAs for Bill to the correct region. And how can I do that for each of the other employees as well?

    Name     Region

 1  Bill     East

 2  Bill     East

 2  Bill     NA

 4  Bill     NA

 5  Karen    NA

 6  Karen    South

 7  Karen    NA

 8  Richard  NA 

 9  Richard  West 

10  Richard  NA

11  Richard  West

1 Answer

0 votes
by (107k points)

You can simply do that with the dplyr and tidyr package and in that use the group_by function and apply to fill as the aesthetic:

library(tidyr) 

library(dplyr)

group_by(df, Name) %>% fill(Region, .direction="downup")

# A tibble: 11 x 2

# Groups:   Name [3]

   Name    Region

   <fct>   <fct> 

 1 Bill    East  

 2 Bill    East  

 3 Bill    East  

 4 Bill    East  

 5 Karen   South 

 6 Karen   South 

 7 Karen   South 

 8 Richard West  

 9 Richard West  

10 Richard West  

11 Richard West  

If you are a beginner and want to know more about R then do check out the following R programming tutorial

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...