2 R语言简介

关于编程,Jeremy Penzer写了一个guidlines,将其摘录如下: > - Problem specification. This is the starting point for any program. Just as you would develop an objective and make an outline if you had to write a paper or report, rather than simply start writing, you should specify the problem as clearly as possible. Particularly important is determining what inputs your program needs and what outputs it should produce. It is also important to consider the problem and its solution from the perspective of those who will use the program and those who will benefit from the solution.

  • Code planning. Contrary to what may be your notion of where to start, it is best not to start writing code, but rather to sketch out a rough version of the program with a pen and paper (or on a computer screen). If you know how to make a flow chart, that is a great idea too. Plan the code by making a nontechnical program prototype. Mock up the desired format of the output.
  • Identify constants. If you plan to use a constant or a variable, it is good to give that value an identifier at the beginning of the program. The advantage is obvious. If you assign a value to an argument or a constant, you can change that value once, and update all instances throughout the program.
  • Program documentation. Good programs are self-documenting. Lay out the code logically and clearly. Use spacing and indentation to make the code readable, and use comments to describe the complicated parts of your program. This helps you and others. When you comment the more complicated parts of your code, you will not have to remember weeks later what was very simple and obvious to you at the time, and you can sometimes spend hours deciphering your own “once-obvious” code. This is the voice of experience speaking here.
  • Solve runtime problems. Although books on programming are usually linear and straightforward, programming in real life is not like that at all. You may break your problem down into several steps, and work on them at different times. You will almost always find that your code does not produce the expected or desired results when you first execute it. You may or may not get much feedback about what went wrong.
  • Verify your program. As we are not developing large or sophisticated programs or functions, we are usually satisfied simply when we use some test cases for which we know the correct answers. More advanced forms of program verification are not needed in this case.

2.1 控制流 (Flow Control)

2.2 循环 (Looping)