R For Dummies. Vries Andrie de

Чтение книги онлайн.

Читать онлайн книгу R For Dummies - Vries Andrie de страница 11

R For Dummies - Vries Andrie de

Скачать книгу

target="_blank" rel="nofollow" href="#i000061190000.jpg" alt="tip"/> If you type the values for the arguments in Help-page order, you don’t have to specify the argument names. You can list the arguments in any order you want, as long as you specify their names.

      Try entering the following example:

      > print(digits = 4, x = 11/7)

      [1] 1.571

      You may wonder where the digits argument comes from, because it’s not explained in the Help page for print(). That’s because it isn’t an argument of the print() function itself, but of the function print.default(). Take a look again at the error you got if you typed print(). R mentions the print.default() function instead of the print() function.

      

In fact, print() is called a generic function. It determines the type of the object that’s passed as an argument and then looks for a function that can deal with this type of object. That function is called the method for the specific object type. In case there is no specific function, R calls the default method. This is the function that works on all object types that have no specific method. In this case, that’s the print.default() function. Keep in mind that a default method doesn’t always exist. We explain this in more detail in Chapter 8. For now, just remember that arguments for a function can be shown on the Help pages of different methods.

      

If you forget which arguments to use, you can find that information in the Help files. Don’t forget to look at the arguments of specific methods as well. You often find a link to those specific methods at the bottom of the Help page. For example, to read the help for the paste() function, type ?paste into your R console.

Making history

      By default, R keeps track of all the commands you use in a session. This tracking can come in handy if you need to reuse a command you used earlier or want to keep track of the work you did before. These previously used commands are kept in the history.

      You can browse the history from the command line by pressing the up-arrow and down-arrow keys. When you press the up-arrow key, you get the commands you typed earlier at the command line. You can press Enter at any time to run the command that is currently displayed.

      Saving the history is done using the savehistory() function. By default, R saves the history in a file called .Rhistory in your current working directory. This file is automatically loaded again the next time you start R, so you have the history of your previous session available.

      If you want to use another filename, use the argument file like this:

      > savehistory(file = "Chapter3.Rhistory")

      

Be sure to add the quotation marks around the filename.

      You can open a file explorer window and take a look at the history by opening the file in any text editor, like Notepad.

      You don’t need to use the file extension .Rhistory – R doesn’t care about extensions that much. But using .Rhistory as a file extension will make it easier to recognize as a history file.

      If you want to load a history file you saved earlier, you can use the loadhistory() function. This will replace the history with the one saved in the .Rhistory file in the current working directory. If you want to load the history from a specific file, you use the file argument again, like this:

      > loadhistory("Chapter3.Rhistory")

      Keeping Your Code Readable

      You may wonder why you should bother about reading code. You wrote the code yourself, so you should know what it does, right? You do now, but will you remember what you did if you have to redo that analysis six months from now on new data? Besides, you may have to share your scripts with other people, and what seems obvious to you may be far less obvious for them.

      Some of the rules you’re about to see aren’t that strict. In fact, you can get away with almost anything in R, but that doesn’t mean it’s a good idea. In this section, we explain why you should avoid some constructs even though they aren’t strictly wrong.

Following naming conventions

      R is very liberal when it comes to names for objects and functions. This freedom is a great blessing and a great burden at the same time. Nobody is obliged to follow strict rules, so everybody who programs something in R can basically do as he or she pleases.

Choosing a correct name

      Although almost anything is allowed when giving names to objects, there are still a few rules in R that you can’t ignore:

      ✔ Names must start with a letter or a dot. If you start a name with a dot, the second character can’t be a digit.

      ✔ Names should contain only letters, numbers, underscore characters (_), and dots (.). Although you can force R to accept other characters in names, you shouldn’t, because these characters often have a special meaning in R.

      ✔ You can’t use the following special keywords as names:

      ● break

      ● else

      ● FALSE

      ● for

      ● function

      ● if

      ● Inf

      ● NA

      ● NaN

      ● next

      ● NULL

      ● repeat

      ● return

      ● TRUE

      ● while

      

R is case sensitive, which means that, for R, lastname and Lastname are two different objects. If R tells you it can’t find an object or function and you’re sure it should be there, check to make sure you used the right case.

Choosing a clear name

      When you start writing code, it’s tempting to use short, generic names like x. There’s nothing wrong with that, as long as it is clear what each object represents. But that might become difficult when all your objects have a single letter name. Likewise, calling your datasets data1, data2, and so forth may be a bit confusing for the person who has to read your code later on, even if it makes all kinds of sense to you now. Remember: You could be the one who, in three months, is trying to figure out exactly what you were trying to achieve. Using descriptive names will allow you to keep your code readable.

      Although

Скачать книгу