0

My code looks like this:

Item | Category
A    |    1
A    |
A    |
A    |    1
A    |
A    |
A    |    1
B    |    2
B    |
B    |
B    |    2
B    |
B    |
B    |    2
B    |
B    |

I want to impute values and fill the "Category" column with the values corresponding to each "Item", wherever it isn't blank. The end result should be like this:

Item | Category
A    |    1
A    |    1
A    |    1
A    |    1
A    |    1
A    |    1
A    |    1
B    |    2
B    |    2
B    |    2
B    |    2
B    |    2
B    |    2
B    |    2
B    |    2
B    |    2

How can I do this in R?

Sotos
  • 51,121
  • 6
  • 32
  • 66
mixedbag99
  • 529
  • 1
  • 4
  • 17
  • Do you have a one to one mapping of item and category? What is your imputation approach? – Roland Mar 14 '17 at 10:02
  • 2
    Would `Category` have same value for every group ? You can replace blanks with `NA` and then use `na.locf`. Something like [this](http://stackoverflow.com/questions/27207162/fill-in-na-based-on-the-last-non-na-value-for-each-group-in-r) – Ronak Shah Mar 14 '17 at 10:03
  • 2
    looks like `zoo::na.locf()` – mtoto Mar 14 '17 at 10:04
  • 1
    are your Categories always the same? (i.e. all 1 for Item A, all 2 for Item B)? If so then something like `df$Category <- with(df, ave(Category, Item, FUN = function(i) i = i[i != ''][1]))` – Sotos Mar 14 '17 at 10:11
  • @RonakShah - thank you! That was exactly what I needed. – mixedbag99 Mar 14 '17 at 10:18
  • @Sotos - thank you! Your solution worked too. – mixedbag99 Mar 14 '17 at 10:18

1 Answers1

1

We can use fill from tidyverse

library(tidyverse)
df1 %>% 
    fill(Category)
akrun
  • 874,273
  • 37
  • 540
  • 662