Skip to content
Prev 370041 / 398503 Next

Fill in empty cell in data.frame from previous value

Hi Chris,
You may know about the *apply family of functions. These slice various
data structures and "apply" a specified function to each slice,
usually returning a list of return values. As far as I am aware, you
can't access adjacent rows unless you reformat the data structure.

There is a way to do this particular job. It requires the sequence
operator (:), the ifelse function and indexing. What you do is to
create a sequence of all the values in the element "names", then
subtract 1 for all the NA (or any other specified value) values and
use the resulting vector to index the original element "names". It
won't work if the first value is NA, nor will it work for more than
one NA in a row. I realize that this is pretty obscure, but you said
that you didn't just want the solution. It's a one-liner.

Jim

On Sun, Jun 25, 2017 at 3:49 AM, Christophe Elek
<christophe.elek at gmail.com> wrote: