Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a equivalent for the tidyr fill() for strings in R?

So I have a data frame like this one:

First Group  Bob
             Joe
             John
             Jesse
Second Group Jane
             Mary
             Emily
             Sarah
             Grace

I would like to fill in the empty cells in the first column in the data frame with the last string in that column i.e

First Group  Bob
First Group  Joe
First Group  John
First Group  Jesse
Second Group Jane
Second Group Mary
Second Group Emily
Second Group Sarah
Second Group Grace

With tidyr, there is fill() but it obviously doesn't work with strings. Is there an equivalent for strings? If not is there a way to accomplish this?

like image 887
Sean Avatar asked Dec 14 '22 14:12

Sean


1 Answers

Seems fill() is designed to be used in isolation. When using fill() inside a mutate() statement this error appears (regardless of the data type), but it works when using it as just a component of the pipe structure. Could that have been the problem?

Just for full clarity, a quick example. Assuming you have a data frame called 'people' with columns 'group' and 'name', the right structure would be:

people %>%
    fill(group)

and the following would give the error you described (and a similar error when using numbers):

people %>%
    mutate(
        group = fill(group)
    )
like image 177
Lucie Kattenbroek Avatar answered Dec 17 '22 23:12

Lucie Kattenbroek