Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fill in blanks based off another column's values in R

Here is the code and resulting dataframe

category <- c("East","BLANK","NorthEast","BLANK","BLANK")
subcat <- c("East","North","SW","NE","SE")
data1 <- as.data.frame(category)
data1$subcat <- subcat


**Category**  **Subcat**
East        East            
BLANK       North           
NorthEast   SW          
BLANK       NE          
BLANK       SE

The East category contains subcat East and North. The NorthEast category contains subcat SW,NE,SE.
As you can see there are blanks for each category. How would I make so the 2nd value in Category is East and 4th and 5th row is North East? I have many more rows in the actual data so a way to do this would be helpful.

The result should be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

**Category**  **Subcat**
East           East         
*East*         North            
NorthEast      SW           
*NorthEast*    NE           
*NorthEast*    SE

>Solution :

We could convert the ‘BLANK’ to NA and use fill

library(tidyr)
library(dplyr)
data1 %>%
   na_if( "BLANK") %>%
   fill(category)

-output

    category subcat
1      East   East
2      East  North
3 NorthEast     SW
4 NorthEast     NE
5 NorthEast     SE
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading