b06-brief_intro_to_R_AC01.Rmd
This chapter provides a brie introduction to R. At the end of are links to additional resources for getting started with R.
You will type R commands into the RStudio console in order to carry out analyses in R. In the RStudio console you will see the R prompt starting with the symbol “>”. “>” will always be there at the beginning of each new command - don’t try to delete it.
We type the commands needed for a particular task after this prompt. The command is carried out after you hit the Return key.
Once you have started R, you can start typing in commands, and the results will be calculated immediately, for example:
Note that prior to the output of “6” it shows “[1]”.
Again, prior to the output of “7” it shows “[1]”.
R can act like a basic calculator that you type command in to. You can also use it like a scientific calcualtor and create variables that store information. All variables (scalars, vectors, matrices, etc.) created by R are called objects. In R, we assign values to variables using an arrow. For example, we can assign the value 2*3 to the variable x using the command:
To view the contents of any R object, just type its name, and the contents of that R object will be displayed:
There are several possible different types of objects in R with fancy math names, including scalars, vectors, matrices, arrays, data frames, tables, and lists. The scalar variable x above is one example of an R object. While a scalar variable such as x has just one element, a vector consists of several elements. The elements in a vector are all of the same type (eg. numeric or characters), while lists may include elements such as characters as well as numeric quantities.
To create a vector, we can use the c() (combine) function. For example, to create a vector called myvector that has elements with values 8, 6, 9, 10, and 5, we type:
To see the contents of the variable myvector, we can just type its name and press enter
The [1] is the index of the first element in the vector. We can extract any element of the vector by typing the vector name with the index of that element given in square brackets. For example, to get the value of the 4th element in the vector myvector, we type:
In contrast to a vector, a list can contain elements of different types, for example, both numeric and character elements. A list can also include other variables such as a vector. The list() function is used to create a list. For example, we could create a list mylist by typing:
We can then print out the contents of the list mylist by typing its name:
The elements in a list are numbered, and can be referred to using indices. We can extract an element of a list by typing the list name with the index of the element given in double square brackets (in contrast to a vector, where we only use single square brackets). Thus, we can extract the second and third elements from mylist by typing:
Elements of lists may also be named, and in this case the elements may be referred to by giving the list name, followed by “\(”, followed by the element name. For example, mylist\)name is the same as mylist[[1]] and mylist$wife is the same as mylist[[2]]:
We can find out the names of the named elements in a list by using the attributes() function, for example:
When you use the attributes() function to find the named elements of a list variable, the named elements are always listed under a heading “\(names”. Therefore, we see that the named elements of the list variable mylist are called “name” and “wife”, and we can retrieve their values by typing mylist\)name and mylist$wife, respectively.
Another type of object that you will encounter in R is a table. For example, if we made a vector variable “mynames” containing the names of children in a class, we can use the table() function to produce a table variable that contains the number of children with each possible name:
Now make the table
Note that there are two Johns and two Marys.
We can store the table variable produced by the function table(), and call the stored table “mytable”, by typing:
To access elements in a table variable, you need to use double square brackets, just like accessing elements in a list. For example, to access the fourth element in the table mytable (the number of children called “John”), we type:
Alternatively, you can use the name of the fourth element in the table (“John”) to find the value of that table element:
Functions in R usually require arguments, which are input variables (ie. objects) that are passed to them, which they then carry out some operation on. For example, the log10() function is passed a number, and it then calculates the log to the base 10 of that number:
In R, you can get help about a particular function by using the help() function. For example, if you want help about the log10() function, you can type:
When you use the help() function, a box or webpage will show up in one of the panes of RStudio with information about the function that you asked for help with. You can also use the ? next to the fuctnion
If you are not sure of the name of a function, but think you know part of its name, you can search for the function name using the help.search() and RSiteSearch() functions. The help.search() function searches to see if you already have a function installed (from one of the R packages that you have installed) that may be related to some topic you’re interested in. The RSiteSearch() function searches all R functions (including those in packages that you haven’t yet installed) for functions related to the topic you are interested in.
For example, if you want to know if there is a function to calculate the standard deviation of a set of numbers, you can search for the names of all installed functions containing the word “deviation” in their description by typing:
Among the functions that were found, is the function sd() in the “stats” package (an R package that comes with the standard R installation), which is used for calculating the standard deviation.
In the example above, the help.search() function found a relevant function (sd() here). However, if you did not find what you were looking for with help.search(), you could then use the RSiteSearch() function to see if a search of all functions described on the R website may find something relevant to the topic that you’re interested in:
The results of the RSiteSearch() function will be hits to descriptions of R functions, as well as to R mailing list discussions of those functions.
We can perform computations with R using objects such as scalars and vectors. For example, to calculate the average of the values in the vector myvector (ie. the average of 8, 6, 9, 10 and 5), we can use the mean() function:
We have been using built-in R functions such as mean(), length(), print(), plot(), etc. We can also create our own functions in R to do calculations that you want to carry out very often on different input data sets. For example, we can create a function to calculate the value of 20 plus square of some input number:
This function will calculate the square of a number (x), and then add 20 to that value. The return() statement returns the calculated value. Once you have typed in this function, the function is then available for use. For example, we can use the function for different input numbers (eg. 10, 25):
To quit R, type:
Some links are included here for further reading.
For a more in-depth introduction to R, a good online tutorial is available on the “Kickstarting R” website, cran.r-project.org/doc/contrib/Lemon-kickstart.
There is another nice (slightly more in-depth) tutorial to R available on the “Introduction to R” website, cran.r-project.org/doc/manuals/R-intro.html.
Chapter 3 of Danielle Navarro’s book is an excellent intro to the basics of R.