A first look to R
The first R session
Start the R system, the cursor is waiting for you to type in some R commands. For example, use R as a simple calculator:
1 |
6 + 3 |
1 |
## [1] 9 |
1 |
5 - 9 |
1 |
## [1] -4 |
1 |
4 * 6 |
1 |
## [1] 24 |
1 |
8 / 3 |
1 |
## [1] 2.667 |
1 |
5 ^ 2 |
1 |
## [1] 25 |
1 |
(1 + 0.05)^8 |
1 |
## [1] 1.477 |
1 |
exp(3) |
1 |
## [1] 20.09 |
1 |
log(14) |
1 |
## [1] 2.639 |
1 |
23.76 * log(8)/(23 + atan(9)) |
1 |
## [1] 2.02 |
R help
Within R, the following functions provide help about R itself:
- The HTML version of R’s online documentation can be printed on-screen by typing
help.start()
; - Online documentation for most of the functions and variables in R can be printed on-screen by typing
help(name)
(or?name
), where name is the name of the topic help is sought for; - Online documentation for finding help pages on a vague topic can be printed on-screen by typing
help.search('topic')
; - A list of function containing topic in the name can be printed on-screen by typing
apropos('topic')
; - A research in the website can be performed by typing
RSiteSearch('query')
, where query is the search query.
To get help about the mean()
function, the help()
function can be used.
1 |
help(mean) |
The help()
function can be called using the ?
.
1 |
?mean |
To get a list of functions concerning the mean, the help.search()
function can be used.
1 |
help.search("mean") |
To get a list of function containing “mean” in the name, the apropos()
function can be used.
1 |
apropos("mean") |
Introducing R Functions
Functions are one of the most important objects in R.
A function is characterised by an input and an output. The input of the function, i.e. the set of the arguments, can be either null or made of one or more R objects. The output of the function can be either null or a single R object. If the function has to return more than one object, the objects are to be inserted in a list.
The function ls()
doesn’t require any input and returns an output.
1 |
ls() |
1 |
## character(0) |
The function rm()
requires an input and doesn’t return an output.
1 2 |
foo = 3 rm(foo) |
Usually, functions require an input and returns an output.
1 |
sum(1:10) |
1 |
## [1] 55 |
The several elements of the input of a function are the arguments of the function. When a function is called the arguments can be declared explicitely or implicitely. The arguments can be explicitely with their name.
1 |
sum(c(1, 2, NA), na.rm = T) |
1 |
## [1] 3 |
In the above example, the na.rm
argument of the sum()
function is called explicitely.
When arguments are called implicitely, the order of the arguments is fundamental. The third example of the code below returns a wrong result.
1 2 |
rdata = c(rnorm(100), NA) quantile(x = rdata, probs = c(0.25, 0.50, 0.75), na.rm = T) |
1 2 |
## 25% 50% 75% ## -0.52412 -0.03614 0.87344 |
1 |
quantile(rdata, c(0.25, 0.50, 0.75), T) |
1 2 |
## 25% 50% 75% ## -0.52412 -0.03614 0.87344 |
1 |
quantile(rdata, T, c(0.25, 0.50, 0.75)) |
1 2 |
## Warning: la condizione la lunghezza > 1 e solo il promo elemento verrà ## utilizzato |
1 2 |
## 100% ## 2.556 |
1 |
quantile(rdata, na.rm = T, probs = c(0.25, 0.50, 0.75)) |
1 2 |
## 25% 50% 75% ## -0.52412 -0.03614 0.87344 |
It is advisable to use the help command of each function to understand not only the arguments which can be used as function inputs, but also the output:
1 |
help(read.table) |
or
1 |
?read.table |
Assignment
Results of calculations can be stored in objects using the assignment operators:
- an arrow (
<-
) formed by a smaller than character and a hyphen without a space; - the equal character (
=
).
These objects can then be used in other calculations. To print the object just enter the name of the object. There are some restrictions when giving an object a name:
- Object names cannot contain “strange” symbols like
!
,+
,-
,#
. - A dot (
.
) and an underscore (_
) are allowed, also a name starting with a dot. - Object names can contain a number but cannot start with a number.
- R is case sensitive:
X
andx
are two different objects, as well astemp
andtemP
.
1 2 3 4 |
x = log(14) y = 23.76 * log(8)/(23 + atan(9)) z = x + y x |
1 |
## [1] 2.639 |
1 |
y |
1 |
## [1] 2.02 |
1 |
z |
1 |
## [1] 4.659 |
The R Workspace
Objects created during an R session are hold in memory. The collection of objects currently avalaible is called the workspace. To list the objects in the current R session, the function ls()
or the function objects()
may be used.
1 |
ls() |
1 |
## [1] "rdata" "x" "y" "z" |
1 |
objects() |
1 |
## [1] "rdata" "x" "y" "z" |
So to run the function ls
the name followed by an opening and a closing bracket must be entered. Entering only ls
will just print the object, i.e. the underlying R code of the function.
Most functions in R accept certain arguments. For example, one of the arguments of the function ls()
is pattern. To list all objects starting with the letter x
:
1 2 |
x2 = log(9) ls(pattern = "x") |
1 |
## [1] "x" "x2" |
If a value to an object that already exists is assigned then the contents of the object will be overwritten with the new value (without a warning!). The function rm()
ought be used to remove one or more objects from your session.
1 2 |
rm(x2) ls() |
1 |
## [1] "rdata" "x" "y" "z" |
The working directory
If you want to read files from a specific location or write files to a specific location you will need to set working directory in R. The following example shows how to set the working directory in R to the folder “Data” within the folder “Documents and Settings” on the C drive.
1 |
setwd("C:/Documents and Settings/Data") |
To get the path of the current working directory, the rgetwd()
function should be used.
1 |
getwd() |
1 |
## [1] "C:/Documents and Settings/Data" |
Remember that you must use the forward slash / or double backslash \\ in R! The Windows format of single backslash will not work.
Summary
In this chapter, we took a look to R. We used R for the first time, as a calculator. We familiarized with R help. We saved the results of calculation in R objects, through assigment. The concept of workspace were introduced. In the next chapter, we’ll look at the types of data R can handle.