Vous êtes sur la page 1sur 4

- : -> returns every integer between two integers.

- If you type an incomplete command and press Enter, R will display a + prompt,
which means it is waiting for you to type the rest of your command:
6+
+ 3
[1] 9
- R will not run anything that follows a hashtag (#) on a line. This makes hashtags
very useful for adding comments and annotations to your code.
- Some R commands may take a long time to run. You can cancel a command once it has
begun by typing ctrl + c.

OBJECTS
- R lets you save data by storing it inside an R object. What�s an object? Just a
name that you can use to call up stored data.
a<-1 #creates the object a that stores the value 1.
a #when you ask R what�s in a, it tells you on the next line
[1] 1
b<-300 #creates the object b that stores the value 300.
b
[1] 300
a+b #you can use your object(s) in new R commands, too. Here, you use the
objects a (1) and b (300) in a sum.
[1] 301
die<-1:6 #creates an object called die, with 6 values, from 1 to 6
die
[1] 1 2 3 4 5 6
- When you create an object, the object will appear in the ENVIRONMENT PANE of
RStudio. This pane will show you all of the objects you�ve created since opening
RStudio.
- Rules for naming objects:
A name cannot start with a number.
A name cannot use some special symbols, like ^, !, $, @, +, -, /, or *.
R understands capitalization, so "a" and "A" are different objects.
The name can be written as "vector" or vector, but if you want a long name
that includes spaces, it is mandatory to use "".
R will overwrite any previous information stored in an object without asking
you for permission. So, it is a good idea to not use names that are already taken.
- ls() -> You can see which object names you have already used. You can also see
those objects in the environment pane
- Element-wise operations: when you manipulate a set of numbers, R will apply the
same operation to each element in the set. So for example, when you run die - 1, R
subtracts one from each element of die.
die-1
[1] 0 1 2 3 4 5
- When you use two or more vectors in an operation, R will line up the vectors and
perform a sequence of individual operations
die*die
[1] 1 4 9 16 25 36
- Vector Recycling: if you give R two vectors of unequal lengths, R will repeat the
shorter vector (it doesn�t change it) until it is as long as the longer vector, and
then do the math. If the length of the short vector does not divide evenly into the
length of the long vector, R will return a warning message.
die+1:2
[1] 2 4 4 6 6 8
- Matrix operations:
Inner multiplication with the %*% operator:
die%*%die
[,1]
[1,] 91

Outer multiplication with the %o% operator:


die%o%die
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 5 6
[2,] 2 4 6 8 10 12
[3,] 3 6 9 12 15 18
[4,] 4 8 12 16 20 24
[5,] 5 10 15 20 25 30
[6,] 6 12 18 24 30 36

t(x) -> transpose of a matrix


det(x,...) -> determinant of a matrix

FUNCTIONS
- A type of object that, instead of storing data, stores code. This code is stored
in a special format that makes it easy to reuse the code in new situations. You can
write your own functions by recreating this format. Examples of functions:
round(x, digits = 0) -> rounds a number to the number of decimals desired
(default: 0 decimals).
factorial(x) -> gives the factorial of a number.
mean(x,...) -> gives the arithmetic mean of a number.
- Argument: The data that you pass into the function. The argument can be raw data,
an R object, or even the results of another R function.
mean(die)
[1] 3.5
round(mean(die))
[1] 4
- sample(x, size, replace = FALSE, prob = NULL) -> takes two arguments: a vector
named x and a number named size. Sample will return size elements from the vector.
When replace is false, the sample is without replacement; when it is true, is with
replacement.
sample(x=die, size=2)
[1] 4 3
sample(x=die, size=2)
[1] 1 2
sample(die, 2) #the names of the arguments are optional, as long as you put
them in the predetermined order, otherwise use names. When functions have too many
arguments, it is convenient to write their names.
[1] 6 3
- args() -> shows you the arguments of the function written between the
parenthesis.
args(sample)
function (x, size, replace = FALSE, prob = NULL) #default values for replace
and prob: FALSE and NULL. Defaults can be overwritten if you input a different
value.
NULL
- Using sample with replacement helps you create independent random samples.
- Simulating the result of rolling a pair of dice:
sample(die, size = 2, replace = TRUE)
- Saving the result on an object (the result won�t change):
dice <- sample(die, size = 2, replace = TRUE)
dice
[1] 6 1
dice
[1] 6 1
dice
[1] 6 1

- function() {} -> makes your own function.


- Every function in R has three basic parts: a name, a body of code, and a set of
arguments. To make your own function, you need to replicate these parts and store
them in an R object, using function.
my_function <- function() {}
- Simulating a pair of dice by writing a function called roll():
roll<-function(){
+ die<-1:6
+ dice<-sample(die,2,TRUE)
+ sum(dice)
+ }
roll()
[1] 5
roll()
[1] 6
roll()
[1] 4
- Typing without parenthesis gives you the code as result:
roll
function(){
die<-1:6
dice<-sample(die,2,TRUE)
sum(dice)
}
<bytecode: 0x0f16a7e8>

ARGUMENTS
- You can give your functions as many arguments as you like. Just list their names,
separated by commas, in the parentheses that follow function. When the function is
run, R will replace each argument name in the function body with the value that the
user supplies for the argument. If the user does not supply a value, R will replace
the argument name with the argument�s default value (if you defined one).
roll2<-function(bones){
+ dice<-sample(bones,2,TRUE)
+ sum(dice)}
roll2(bones=1:4)
[1] 4
roll2(bones=1:6)
[1] 10
roll2(bones=1:20)
[1] 15
roll2()
Error in sample(bones, 2, TRUE) :
argument "bones" is missing, with no default #this error happens because
there�s no default value for bones

roll2<-function(bones=1:6){
+ dice<-sample(bones,2,TRUE)
+ sum(dice)}
roll2()
[1] 7
roll2()
[1] 9
roll2(bones=1:20)
[1] 21
- To summarize, function helps you construct your own R functions. You create a
body of code for your function to run by writing code between the braces that
follow function. You create arguments for your function to use by supplying their
names in the parentheses that follow function. Finally, you give your function a
name by saving its output to an R object.

SCRIPTS
- File > New File > R script.
- File > Save as.
- Highlight what you want to run in the script or put the cursor on the line you
want to run and click Run button.
- Run the whole script with the Source button.
- RStudio comes with a tool that can help you build functions. To use it, highlight
the lines of code in your R script that you want to turn into a function. Then
click Code > Extract Function in the menu bar. RStudio will ask you for a function
name to use and then wrap you code in a function call.
- Check for errors in the extracted function.

Vous aimerez peut-être aussi