To achieve this in R, you can use the tidyverse packages, specifically dplyr and tidyr. Here's how you can do it:
main.r213 chars9 lines
However, the above code does not specifically target the last non-NA value. For that, you need a custom function that can be applied to each row.
Here is a more detailed example:
main.r503 chars22 lines
This will correctly replace the last non-NA value in the columns starting with "to" with the value from other_column.
Remember to replace "other_column" with the name of the column that contains the string you want to use for replacement.
Please note that the rowwise function may not be efficient for large datasets due to its overhead. For large datasets, using a vectorized approach or data.table package may be more efficient.
Here's how you could achieve the same using the data.table package:
main.r295 chars14 lines
gistlibby LogSnag