Computers:
- Let you assemble, manipulate and visualize data sets
A data scientist who knows how to program→ improve the ability to:
- Memorize (store) entire data sets
- Recall data values on demand
- Perform complex calculations with large amounts of data
- Do repetitive tasks without becoming careless or bored
2.1the R user interface:
What can you do:
- Enter: run the code
- Ctrl + C: cancel a command once it has begun (it may also take long to cancel the comman)
What you can type:
- Command: the code you type. It will command your computer to do something for you
- Command line: the line you type the command into
- Colon operator (:) : returns every integer between two integers. It is an easy way to create a sequence of
numbers
- # (known as commenting symbol): R will not run anything that follows a hashtag on a line and allows to add
comments and annotations to the code
Bracketed numbers:
Left: Below you see that [1] appears next to the result → R is then letting you know that this line begins with the first
value in your result.
Right: on the right picture you can see that [14] appears and this means that you start with the 14th value in the
result. Same for the [25] this means that it starts with the 25th number of the sequence.
Incomplete command:
- If you type an incomplete command and press enter, R will type a + for you because it knows something is
missing and therefor it is waiting for you to type the rest of your command
Arithmetic:
- You can use R as a calculator for example to calculate 6/(4-1)
Hashtag (#)
- R will not run anything that follows a hashtag on a line → very useful for adding comments and annotations
to your code
2.2Objects:
What you can type:
- Colon operator (:) : create a group of numbers
- Object: you can save a vector of numbers by using this and this is a name that you can use to call up stored
data
- 1s: you can see which object names you have already used
- t: transpose a matrix
- det: take its determinant
1
,The : operator returns its results as a vector, a one-dimensional set of numbers → So 1:6 results in 1 2 3 4 5 6 and this
is called the vector of numbers
Object:
- A name that you can use to call up stored data
- So for example you can save data into an object like a (as you can see in the picture) and wherever R
encounters the object a, it will replace it with the data saved inside
- You can name an object in R almost anything you want, but there are rules:
o Cannot start with a number
o Cannot use some special symbols like
- Watch out when making an object with the capitalization; name is something different for R than Name
- R will overwrite any previous information stored in an object without asking you for permission → do not use
names that are already taken
- R will replace an object with its content (what is stored in the environment pane) with its contents whenever
the object’s name appears in a command.
SO:
1. To create an R object, choose a name and then use the less-than symbol (<) , followed by a minus sign (-), to
save data into it. R will make an object, give it your name, and store it in whatever follows the arrow.
a. A <- stores 1 in an object named A
2. When you ask R what’s in A, R tells you on the next line
3. You can use your object in new R commands, too. Since A previously stored the value of 1, you are now
adding 1 to 2.
Environment pane:
- This pane will show you all of the objects you have created since opening R studio
- This is the right upper pane
1s:
- By using this function you can see which object names you have already used.
You can do all sorts of math with the die (1:6) and when typing die R will replace an object with its contents:
- Math isn’t so helpful for rolling dice, but manipulating sets of numbers will be your stock and trade as a data
scientist.
→ so you do 1 2 3 4 5 6 and all -1 so you get 1-1=0 and 2-1=1 etc.
→ so you do 1 2 3 4 5 6 and all /2 so you get 1/2=0,5 and 2/2=1 etc.
→ so you do 1 2 3 4 5 6 and all times die so 1*1 = 1 and 4*4=16 etc. → use two or more vectors
SO: Element wise-execution:
- When you manipulate a set of numbers, R will apply the same operation to each element in the set → R
doesn’t always follow the rules of matrix multiplication
- This means that when you run die-1, R subtracts 1 from each element of die! ( this is the same thing as you
can see above in the example)
Use two or more vectors:
- For example when you run die*die , R lines up the two die vectors and then multiplies the first element of
vector 1 by the first element of vector 2. R then multiplies the second element of vector one with the second
element of vector 2 etc.
2
, → When R performs element-wise execution, it matches up vectors and then manipulates each pair of elements independently.
Vector recycling: If you give R two vectors of unequal lengths, R will repeat the shorter vector until it is as long as
the longer vector.
- This isn’t permanent → shorter vector will be its original size after R does the math.
- It helps R do element-wise operations. These operations are a very useful feature in R because they
manipulate groups of values in an orderly way. They will ensure that values from one observation or case are
only paired with values from the same observation or case.
R can also do traditional matrix multiplication:
- You have to ask R to do this
- You can do inner multiplication with the %*% operator and outer multiplication with the %o%
2.3functions: are needed to roll the die → you’ll need to randomly select one of the die’s
values. And for that you will need a function
What you can type:
- Round: round a number
- Factorial: you can calculate its factorial
- Sample: this takes two arguments; a vector named x and a number named size
- Args(name of the function): look up the function’s arguments
- Replace=TRUE; causes the sample to sample with replacement, instead of without → sample may select the
same value on the second draw.
- Sum: add up the numbers
How to use a function:
- Just write the name of the function and then the data you want the function to operate on in parentheses
3
, Argument:
- The data that you pass into the function
- The argument can be:
o Raw data
o An R object
o The results of another R function
▪ In this case R will work from the innermost function to the outermost (as you can see in the
picture)
→ you can see that R first looks up die, then calculates the
mean of one through six and then rounds the mean→ this
means that when you links together, R will resolve them from
the innermost operation to the outermost
How to roll the die;
- With R’s sample function. Sample takes two arguments: a vector named x and a number named size. Sample
will return size elements from the vector
- To roll the die and get a number back: set x to die and sample one element from it → you then get a new
(maybe different) number each time you roll it
can also be written as → (every argument in every R function has a name. You
can specify which data should be assigned to which argument by setting a name equal to the data, as in the preceding
code. This becomes important as you begin to pass multiple arguments to the same function; names help you avoid
passing the wrong data to the wrong argument. You notice that r users do not often use the name of the first argument
in a function.
How do you know which argument names to use?
- Use a name that a function does not expect → ERROR
o
- Look up function’s arguments with args
o You place the name of the function in the parentheses () behind args
o → round function takes two arguments, one named X and one named digits
default value:
- The digits argument of round is already set to 0 → R function will take optional arguments like digits. These
arguments are considered optional because they come with a default value. You can pass a new value to an
optional argument if you want, and R will use the default value if you do not
To override the default, you can supply your own value for digits (upper is with the
default value and lower with overridden default) →
Write out the names of each argument after the first one or two when you call a function with multiple arguments
because:
- It helps you and others understand you code
- Obvious which argument your first input refers to (sometimes second input as well), but need a large memory
to remember the third and fourth arguments of every R function
4
Voordelen van het kopen van samenvattingen bij Stuvia op een rij:
Verzekerd van kwaliteit door reviews
Stuvia-klanten hebben meer dan 700.000 samenvattingen beoordeeld. Zo weet je zeker dat je de beste documenten koopt!
Snel en makkelijk kopen
Je betaalt supersnel en eenmalig met iDeal, creditcard of Stuvia-tegoed voor de samenvatting. Zonder lidmaatschap.
Focus op de essentie
Samenvattingen worden geschreven voor en door anderen. Daarom zijn de samenvattingen altijd betrouwbaar en actueel. Zo kom je snel tot de kern!
Veelgestelde vragen
Wat krijg ik als ik dit document koop?
Je krijgt een PDF, die direct beschikbaar is na je aankoop. Het gekochte document is altijd, overal en oneindig toegankelijk via je profiel.
Tevredenheidsgarantie: hoe werkt dat?
Onze tevredenheidsgarantie zorgt ervoor dat je altijd een studiedocument vindt dat goed bij je past. Je vult een formulier in en onze klantenservice regelt de rest.
Van wie koop ik deze samenvatting?
Stuvia is een marktplaats, je koop dit document dus niet van ons, maar van verkoper valeriedinjens1. Stuvia faciliteert de betaling aan de verkoper.
Zit ik meteen vast aan een abonnement?
Nee, je koopt alleen deze samenvatting voor €7,99. Je zit daarna nergens aan vast.