don't type it twice

31 Aug 2015

In interactive data analysis I often update a variable after performing some operation on it

    variable_with_long_name <- f(variable_with_long_name)

The process is to first play with f for a while, and then when it is right to add the assignment. Using meaningful variable names is somewhat discouraged by the typing, and, maybe more important, when reading you have more text to compare to check it really is an update. magrittr to the rescue. While looking for the right function use

    variable_with_long_name %>% f

Then when it is correct update the %>% to %<>% to get

    variable_with_long_name %<>% f

giving a clear, update this variable by applying function f. This also works well when using data.table. I use the following to zoom a table down to a single customer

    table_of_interest %<>% .[custid == "CUST1"]