I have a data frame for which I would like to complete the columns from left to right by using the lasted non NA value and adding (foo)
suffix after it. For example, this data frame:
df <- data.frame(
x = c("one", "one", "three"),
y = c("two", "four", NA),
z = c("three", NA, NA)
)
df
#> x y z
#> 1 one two three
#> 2 one four <NA>
#> 3 three <NA> <NA>
would produce:
data.frame(
x = c("one", "one", "three"),
y = c("two", "four", "three (foo)"),
z = c("three", "four (foo)", "three (foo)")
)
#> x y z
#> 1 one two three
#> 2 one four four (foo)
#> 3 three three (foo) three (foo)
Is there an elegant way to do it? It can be base R, tidyverse or data.table solution. Created on 2019-06-26 by the reprex package (v0.3.0)