To achieve this in R, you can use the dplyr
and tidyr
packages along with some basic R operations. Here's an example of how you might approach this task. Assume you have a dataset (dataframe) named df
and you want to replace the last non-NA string in each row (across columns that start with 'to') with the string from another column, let's say another_column
.
main.r819 chars33 lines
This code snippet does the following:
df
with columns that start with 'to' and another column.replace_last_non_na
that identifies the last non-NA value in columns starting with 'to' for each row, and then replaces this value with the value from another_column
.apply
.Remember, the specifics can vary based on the exact structure of your dataset and the versions of the packages you're using. Always make sure to test such operations on a small subset of your data before applying them to the entire dataset.
gistlibby LogSnag