R Intro Edx-Datacamp
R Intro Edx-Datacamp
2nd chapter not available for free at datacamp but it does at edx, and goes as follows
The workspace
If you assign a value to a variable, this variable is stored in the workspace. It's the
place where all user-defined variables and objects live. The command ls() lists
the contents of this workspace. rm(<var_name>) allows you to remove objects from
the workspace again. Try the following code in the console:
a <- 1
b <- 2
ls()
rm(a)
ls()
The first two lines create the variables a and b. Calling ls()now shows you
that a and b are in the workspace. After removing a using rm(a), the
same ls() command will show you that only b remains in the workspace. You
could also remove both a and b in a one-liner: rm(a,b).
The first line of the sample code is rm(list = ls()). This is a very useful
command to clear everything from your workspace!
Instructions
100 XP
Instructions
100 XP
List the contents of the workspace to check that the workspace is empty.
Create a variable, horses, equal to 3.
Create another variable, dogs, which you set to 7.
Create a new variable, animals, that is equal to the sum of horses and dogs.
Inspect the contents of the workspace again to see that indeed, these three
variables are available.
Eliminate the dogs variable from the workspace.
Finally, inspect the objects in your workspace once more to see that
only horses and animals remain.
Take Hint (-30 XP)
>
> ls()
character(0)
>
>
>
>
> ls()
>
> rm(dogs)
>
> ls()
>Awesome! Now that you know how you can build up, inspect and manage your workspace, it's
time for your first challenge!
V=2π2r2RV=2π2r2R
where rr is the minor radius and RR is the major radius. This is the same as
computing the area of the cylindrical portion of the donut ( πr2πr2) and multiplying it
by the circumference of the donut (2πR2πR). Top off this theory with some
workspace management and you've got one tasty challenge! One last tip: ππ is
available in R by default as pi.
Instructions
200 XP
Instructions
200 XP
Define the variables r (inner radius) and R (outer radius) and set them to 2 and 6
using the assignment operator (<-).
Calculate the volume of the donut based on the formula above and assign the
result to vol_donut. You can use intermediary variables for this if you want.
Remove all intermediary variables that you've used to calculate vol_donut using
the rm() function.
Finally, use ls() to list the elements in your workspace. Only vol_donut should
remain in your workspace at this point.
Take Hint (-60 XP)
> r <- 2
> R <- 6
>
>
> # Remove all intermediary variables that you've used with rm()
> rm(pi_2)
> rm(r_2)
> rm(r)
> rm(R)
>
[1] "vol_donut"
>Awesome! In this exercise, the true power of variables became apparent. Close this window to
head back to edX and continue to learn more about R's basic data types.
ANOTHER CHAPTER
Take Hint (-30 XP)
>
> # Convert var1 to a character: var1_char
>
> is.character(var1_char)
[1] TRUE
>
>
> class(var2_log)
[1] "logical"
>
>Bellissimo! The final coercion you tried did not succeed, hence the warning. Head over to the
challenge that concludes this chapter.
Take Hint (-60 XP)
> ls()
>
>
>Perfect! Sit back and relax for a while after this first introduction to R, but not for too long: there
is much more to come! Close this tab and head over to edX again to learn more about vectors.
ANOTHER CHAPTER……
You better, because this chapter takes you on a trip to Sin City, also known
as "Statisticians Paradise" ;-).
Thanks to R and your new data science skills, you will learn how to uplift your
performance at the tables and fire off your career as a professional gambler. This
chapter will show how you can easily keep track of your betting progress and how
you can do some simple analyses on past actions. Next Stop, Vegas Baby...
VEGAS!!
On your way from rags to riches, you will make extensive use of vectors. As Filip
explained you, vectors are one dimensional arrays that can hold numeric data,
character data, or logical data. You create a vector with the combine function c().
You place the vector elements separated by a comma between the brackets. For
example:
Take Hint (-30 XP)
# Create boolean_vector
Perfect! Notice that adding a space behind the commas in the c() function improve the readability
of your code. Let's practice some more with vector creation in the next exercise.
Before doing a first analysis, you decide to first collect all the winnings and losses
for the last week:
For poker_vector:
For roulette_vector:
On Monday you lost \$24
Tuesday you lost \$50
Wednesday you won \$100
Thursday you lost \$350
Friday you won \$10
You only played poker and roulette, since there was a delegation of mediums that
occupied the craps tables. To be able to use this data in R, you decide to create
the variables poker_vector and roulette_vector.
Instructions
100 XP
Instructions
100 XP
Very good! To check out the contents of your vectors, remember that you can always simply type
the variable in the console and hit Enter. Proceed to the next exercise!
In the previous exercise, we created a vector with your winnings over the week.
Each vector element refers to a day of the week but it is hard to tell which element
belongs to which day. It would be nice if you could show that in the vector itself.
Remember the names()function to name the elements of a vector?
some_vector <- c("Johnny", "Poker Player")
names(some_vector) <- c("Name", "Profession")
You can accomplish the exact same thing by using the equals sign inside c():
some_vector <- c(Name = "Johnny", Profession = "Poker Player")
Instructions
100 XP
Go ahead and assign the days of the week as names
to poker_vector and roulette_vector. In case you are not sure, the days of the
week are: Monday, Tuesday, Wednesday, Thursday and Friday.
Take Hint (-30 XP)
In the previous exercises you probably experienced that it is boring and frustrating
to type and retype information such as the days of the week. However, there is a
more efficient way to do this, namely, to assign the days of the week vector to a
variable!
Just like you did with your poker and roulette returns, you can also create a
variable that contains the days of the week. This way you can use and re-use it.
Instructions
100 XP
Instructions
100 XP
Take Hint (-30 XP)
# Poker winnings from Monday to Friday
Nice one! A word of advice: try to avoid code duplication at all times. Continue to the next exercise
and learn how to do arithmetic with vectors!
Instructions
100 XP
Possible Answers
Submit Answer
Take Hint (-30 XP)
Correct! You might expect that the names of the
vectors roulette_vector1 and roulette_vector2 are named the same; but the different approaches
treat missing name information differently. Also, notice here how you can also
use names() to get the names of a vector! Head back to edX by closing this tab.
CHAPTER 3
How much has been your overall profit or loss per day of the week?
Have you lost money over the week in total?
Are you winning/losing money on poker or on roulette?
You'll have to do arithmetic calculations on vectors to solve these problems.
Remember that this happens element-wise; the following three statements are
completely equivalent:
c(1, 2, 3) + c(4, 5, 6)
c(1 + 4, 2 + 5, 3 + 6)
c(5, 7, 9)
Instructions
100 XP
Instructions
100 XP
Take Hint (-30 XP)
# A_vector and B_vector have already been defined for you
A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)
# Print total_vector
total_vector
# Print diff_vector
diff_vector
Calculate your earnings
Now that you understand how R does arithmetic calculations with vectors, it is time
to get those Ferraris in your garage! First, you need to understand what the overall
profit or loss per day of the week was. The total daily profit is the sum of the
profit/loss you realized on poker per day, and the profit/loss you realized on
roulette per day.
Instructions
100 XP
Assign to the variable total_daily how much you won or lost on each day in total
(poker and roulette combined).
Take Hint (-30 XP)
You can answer this question using the sum() function. As mentioned in the video,
it calculates the sum of all elements of a vector.
Instructions
100 XP
Instructions
100 XP
Calculate the total amount of money that you have won/lost with poker and
assign it to the variable total_poker.
Do the same thing for roulette and assign the result to total_roulette.
Now that you have the totals for roulette and poker, you can easily
calculate total_week (which is the sum of all gains and losses of the week).
Print the variable total_week.
Take Hint (-30 XP)
> # Casino winnings from Monday to Friday
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
>
> # Total winnings with poker: total_poker
> total_poker <- sum(poker_vector)
>
> # Total winnings with roulette: total_roulette
> total_roulette <- sum(roulette_vector)
>
> # Total winnings overall: total_week
> total_week <- total_poker + total_roulette
>
> # Print total_week
> total_week
[1] -84
Oops, it seems like you are losing money. Time to rethink and adapt your strategy!
This will require some deeper analysis…
Comparing total winnings
The previous exercise showed that you are losing money, now what? After a short
brainstorm in your hotel's jacuzzi, you realize that a possible explanation might be
that your skills in roulette are not as well developed as your skills in poker. You
choose to use the >operator to reveal this.
Instructions
100 XP
Instructions
100 XP
Take Hint (-30 XP)
> # Casino winnings from Monday to Friday
> poker_vector <- c(140, -50, 20, -120, 240)
> roulette_vector <- c(-24, -50, 100, -350, 10)
> days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
> names(poker_vector) <- days_vector
> names(roulette_vector) <- days_vector
>
> # Calculate poker_better
> poker_better <- poker_vector>roulette_vector
>
> # Calculate total_poker and total_roulette, as before
> total_poker <- sum(poker_vector)
> total_roulette <- sum(roulette_vector)
>
> # Calculate choose_poker
> choose_poker <- total_poker>total_roulette
>
> # Print choose_poker
> choose_poker
[1] TRUE
>Great! Your hunch seemed to be right. It appears that the poker game is more
your cup of tea than roulette. Ready for a challenge? Head over to the next
exercise!
Instructions
200 XP
Instructions
200 XP
Take Hint (-60 XP)
> # Calculate total gains for your entire past week: total_past
> total_past <- c(sum(poker_past) + sum(roulette_past))
>
> # Difference of past to present performance: diff_poker
> diff_poker <- c(poker_present - poker_past)
> diff_poker
Monday Tuesday Wednesday Thursday Friday
210 -140 -90 0 210
Awesome! It seems that indeed, your roulette skills have worsened if you compare
to your previous week in Vegas. Go back to edX to learn about new ways of
investigating your gambling performance.
You have finished the chapter "Vector Arithmetic"!
CHAPTER 4
Take Hint (-30 XP)
>
Wednesday
20
>
> roulette_friday
Friday
10
>Great! R also makes it possible to select multiple elements from a vector at once, remember? Put
the theory to practice in the next exercise!
Instead of using a single number to select a single element, you can also select
multiple elements by passing a vector inside the square brackets. For example,
poker_vector[c(1,5)]
selects the first and the fifth element of poker_vector.
Instructions
100 XP
Instructions
100 XP
Take Hint (-30 XP)
>
> poker_midweek
-50 20 -120
> roulette_endweek
Thursday Friday
-350 10
>Well done! Continue to the next exercise to specialize in vector selection some more!
Take Hint (-30 XP)
> # Casino winnings from Monday to Friday
> poker_vector <- c(140, -50, 20, -120, 240)
>
>
> roulette_subset
Awesome! The colon operator is extremely useful and very often used in R programming, so
remember it well. Have you noticed that the elements in poker_vector and roulette_vector also
have names associated with them? You can also subset vectors using these names, remember?
poker_vector["Monday"]
will select the first element of poker_vector since "Monday" is the name of that first
element.
Instructions
100 XP
Instructions
100 XP
Take Hint (-30 XP)
> # Casino winnings from Monday to Friday
>
>
>
roulette_vector[c("Monday","Wednesday")]
Of course you can't use the colon trick here: "Monday":"Wednesday" will generate
an error.
Instructions
100 XP
Create a vector containing the poker gains for the first three days of the week;
name it poker_start.
Using the function mean(), calculate the average poker gains during these first
three days. Assign the result to a variable avg_poker_start.
Take Hint (-30 XP)
>
>
> # Calculate the average poker gains during the first three days: avg_poker_start
>Good job! Next to subsetting vectors by index or by name, you can also use logical vectors. The
next exercises will test you on this.
# selection by name
poker_vector[c("Monday", "Wednesday")]
# selection by logicals
poker_vector[c(TRUE, FALSE, TRUE, FALSE, FALSE)]
Instructions
100 XP
Instructions
100 XP
Assign the roulette results from the first, third and fifth day to roulette_subset.
Select the first three days from poker_vector using a vector of logicals. Assign the
result to poker_start.
Take Hint (-30 XP)
>
>
Nice one! Using logical vectors to perform subsetting might seem somewhat tedious, but its true
power will become clear in the next exercise!
Experiment with these operators in the console. The result will be a logical vector,
which you can use to perform subsetting! This means that instead of selecting a
subset of days to investigate yourself like before, you can simply ask R to return
only those days where you realized a positive return for poker.
Instructions
100 XP
Instructions
100 XP
Check if your poker winnings are positive on the different days of the week (i.e. >
0), and assign this to selection_vector.
Assign the amounts that you won on the profitable days, so a vector, to the
variable poker_profits, by using selection_vector.
Take Hint (-30 XP)
>
> selection_vector
>
> poker_profits
140 20 240
>
Take Hint (-30 XP)
>
>
>
>
> roulette_profits
Wednesday Friday
100 10
> roulette_total_profit
[1] 110
> num_profitable_days
[1] 2
> Awesome! If you inspect the variable num_profitable_days, you'll see that is equal to 2, meaning
that you only had two profitable roulette days. You can conclude that roulette is not your game,
right?
The sums for the player's last 7 games are stored in player; the house's scores
are contained in house. Both are available in the workspace. In both cases, the
scores were never higher than 21.
Instructions
200 XP
Instructions
200 XP
With square brackets, select the player's score for the third game, using any of the
techniques that you've learned about. Store the result in player_third.
Subset the player vector to only select the scores that exceeded the scores
of house, so the scores that had the player win. Use subsetting in combination with
the relational operator >. Assign the subset to the variable winning_scores.
Count the number of times the score inside player was lower than 18. This time,
you should use a relational operator in combination with sum(). Save the resulting
value in a new variable, n_low_score.
Take Hint (-60 XP)
> # Select the player's score for the third game: player_third
>
>
> player_third
[1] 20
> winning_scores
[1] 17 21 18
> n_low_score
[1] 3
> Awesome! This exercise concludes the chapter on vectors. The next module will introduce you
to the two-dimensional version of vectors: matrices. Close this tab to continue your learning on
edX.