Apply fill() to all columns in data frame



You’ve carefully parsed your log file, created feature columns, and now just want to propagate all known values down all columns. What’s the neat way to do this?

I have an ID column (I don’t care about) and the help for select lead me to my best solution so far of
blah_df %>% fill(-ID)

Positive values select variables; negative values to drop
variables. If the first expression is negative, ‘select()’
will automatically start with all variables.

But this requires the presence of a column you don’t want to fill. I’d kinda hope that fill() would default to all columns rather than none.


Would you mind building this out as a full minimal reproducible example (aka a reprex)?

It’ll be much easier to troubleshoot/discuss if everyone’s on the same page with what this looks like (as Jenny Bryan ~put it, conversations about code are much easier with code).

If you’re not familiar with reprexes, I really like the Magic Reprex post by Nick Tierney.


I think I understand although I think @mara’s advice is smart, re: reprex.

Try this:
df %>% fill(names(df))


Thanks @pedram. That’s exactly the sort of thing I had in mind, although I’d turn it into
df %>% fill(names(.))

I’m totally down with @mara’s reprex in general for problems (I only came across it today on the tidyverse website). But for a generic “How do you select all columns in a fill()?” question, a reproducible example felt a bit like overkill. But I shall be playing with reprex!


Once you’ve played with it, you’ll see that it takes almost no time at all…you can literally just highlight your code and hit the render selection as reprex (my wording may be off) addin (or whatever method you like), and the input and output are pasted to your clipboard.

My suggestion has less to do with complexity, and more with the probability of getting your question answered. Obviously I turned out to be wrong in this case, but I’m a reprex die hard.


Oh, also, if your question’s been answered, would you mind ticking the solution box?

Discourse Solved Accepted Answer Plugin


If I’m not being nagged by @discobot, it’s by @mara
:tongue in cheek emoji:
:heart: @mara’s posts!


Hi! To find out what I can do, say @discobot display help.


You know, @discobot, you could have warned me the “mark as solution” checkbox kinda already looks checked.


I’m hoping it’ll catch on, but it’s proving to be a bit more difficult than I thought…
stop trying to make fetch happen)


I totally get it. I manage a community and no one there ever ticks “answered”. Fortunately I easily keep on top of posts as I get notified. I just didn’t know about this one. I do now!