Now that you know how to use R, let’s use it to make a virtual die. I’ll avoid showing >s and s unless I want you to look at them. I’ll use a single hashtag to add my own comments and a double hashtag, #, to display the results of code. The hashtag is known as the commenting symbol in R.įor the remainder of the book, I’ll use hashtags to display the output of R code. Humans will be able to read the comments, but your computer will pass over them. This makes hashtags very useful for adding comments and annotations to your code. R treats the hashtag character, #, in a special way R will not run anything that follows a hashtag on a line. This will make the code easier to copy and paste if you want to put it in your own console. For example, you could do some basic arithmetic: 2 * 3ĭid you notice something different about this code? I’ve left out the >’s and ’s. Once you get the hang of the command line, you can easily do anything in R that you would do with a calculator. You can then try a different command at the next prompt: > 3 % 5 R is just telling you that your computer couldn’t understand or do what you asked it to do. If you ever see an error message, don’t panic. If you type a command that R doesn’t recognize, R will return an error message. Either finish the command or hit Escape to start over: > 5. If you type an incomplete command and press Enter, R will display a + prompt, which means R is waiting for you to type the rest of your command. R is a dynamic programming language, which means R automatically interprets your code as you run it. If you’ve programmed in such a language before, you may wonder whether you have to compile your R code before you can use it. In some languages, like C, Java, and FORTRAN, you have to compile your human-readable code into machine-readable code (often 1s and 0s) before you can run it. You can mostly ignore the numbers that appear in brackets: > 100:130 These numbers just mean that the second line begins with the 14th value in the result, and the third line begins with the 25th value. Notice that new bracketed numbers appear at the start of the second and third lines of output. For example, the command 100:130 returns 31 values it creates a sequence of integers from 100 to 130. Some commands return more than one value, and their results may fill up multiple lines. R is just letting you know that this line begins with the first value in your result. You’ll notice that a appears next to your result. For example, if you type 1 + 1 and hit Enter, RStudio will display: > 1 + 1 Then RStudio displays a fresh prompt for your next command. When you type a command at the prompt and hit Enter, your computer executes the command and shows you the results. The line you type it into is called the command line. The code you type is called a command, because it will command your computer to do something for you. You type R code into the bottom line of the RStudio console pane and then click Enter to run it. The appendix will give you an overview of the two free tools and tell you how to download them. Various other data types return slightly different results.If you do not yet have R and RStudio intalled on your computer–or do not know what I am talking about–visit Appendix A. Results of the str() function on the sample data set PlantGrowth.įor a vector, str() tells you how many items there are - for 8 items, it'll display as - along with the type of item (number, character, etc.) and the first few entries. This will tell you the type of object you have in the case of a data frame, it will also tell you how many rows (observations in statistical R-speak) and columns (variables to R) it contains, along with the type of data in each column and the first few entries in each column. To quickly see how your R object is structured, you can use the str() function: Tail can be useful when you've read in data from an external source, helping to see if anything got garbled (or there was some footnote row at the end you didn't notice). To see the last few rows of your data, use the tail() function: Note: If your object is just a 1-dimensional vector of numbers, such as (1, 1, 2, 3, 5, 8, 13, 21, 34), head(mydata) will give you the first 6 items in the vector. Want to see, oh, the first 10 rows instead of 6? That's: R will display mydata's column headers and first 6 rows by default.
0 Comments
Leave a Reply. |