Group columns where some rows have the same values

Hi everyone! I have a dataset based on dead and alive plants over time. Since some plants have died earlier than others, these do not include data from the time points after they've died. Therefore, I have created a new data frame that includes all time steps also for the dead plants. The problem is that all extra rows in the table have NA values. For example, I am trying to have a table with all begin length per plant. In the picture you can see that after the plant died, no data is available anymore, so I want to print the begin lengths for every time step.
I have tried fixing this mostly with the ifelse() function, however this is a lot of work (since I have 144 plants). Does anyone have an idea how I can do this more easily? Thanks in advance.


Hi @Almira
Welcome to the Posit/RStudio Community Forum.

Try something like this:

data <- data %>% 
  group_by(plant) %>%
  fill(length..t0.)  %>%
  fill( %>%

The default fill direction is downwards.

Hi @DavoWW
Thank you for your response and suggestion.
I have installed the package "tidyverse", however, the function "fill" could not be found. Do you (or somebody else) know how I can fix this? Many thanks.

The fill() function is in the tidyr package. Did you run library(tidyverse) to load all of the packages in the core tidyverse, including tidyr?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.