Dear Professor Mean,
I’m just getting started with R and want to know how to create a good working program.
Loyal Reader
Dear Reader,
There are two answers.
First, start by finding something that is close to what you want to do. The closer the better, but you don’t have to be very close. Once you have something that works, rewrite parts of the code, delete other parts of the code, and add new parts of the code.
Second, start by writing an outline of the various steps that you expect to need. It doesn’t matter if you make a mistake here. Just put down your best guess. Then write chunks of code for each step. if you find out that you left out a step, it is okay to add that later. If you don’t need a step, just get rid of it. if you find that you are paining yourself into a corner or going down a blind alley, back up a bit in the list of steps and write down a different list of steps that will keep you out of that alley or corner.
For both approaches, make sure that you document things well. In particular, if you have something a bit unusual for you, add a comment about what this does and maybe include a link. This will help when you look at your code a few months from now or a few years from now.