Get startedGet started for free

Welcome to Introduction to R for Finance!

1. Welcome to Introduction to R for Finance!

Hi! My name is Lore, and I am data scientist at DataCamp.

2. A Hands-On Course

DataCamp is an online data science school. Here, you'll take fun video lessons and solve interactive coding challenges, where you'll receive instant and detailed feedback.

3. What will you learn?

In this introduction to R for finance course, you will learn the basics of data analysis in R, along with the common structures that are used to store data. Unique to this course, almost all examples will be related to finance, allowing you to have concrete applications of the theory and ideas that you learn along the way. Great! Now that the introduction is out of the way, it's time to get started with the basics. As you will soon learn, R can be used to perform mathematical calculations.

4. Console

An important component of R is the console. The R Console is where you can execute R commands. In DataCamp's interactive interface, the console can be found here.

5. Console

Let's try to calculate the sum of 1 and 2. We simply type 1 + 2 at the prompt and hit Enter. R interprets what you typed and prints the result. Pretty cool, right?

6. Variables or objects

R is much more than just a fancy calculator. It is a powerful tool for doing statistical data analysis. One great feature for this is the ability to create _variables_, often also referred to as _objects_. Think of variables as containers to store your data for later use. After creating your variable,

7. Variables or objects

you can run other lines of code, and whenever you want to get your data back,

8. Variables or objects

you simply type the name of the variable you saved it in. To create a variable in R, use the less than sign, followed by a dash. This operation is known as assignment. What would this example look like in the R console? Type my_number, assign 5 to it, and press enter. When you want to access the data in my_number, type in it's name, press enter again, and R returns 5.

9. Arithmetic in R

As another example, assume your good friend Dan owes you 100 dollars. To keep track of this, you can create a variable named Dan and assign the value of 100 to it. If your other friend Rob owes you 50, you can repeat the process by creating a variable for Rob and assigning it 50. Suppose you want to calculate the total amount your two friends owe you. Luckily for you, in R you can perform calculations with variables. To calculate the total, just type 'dan + rob'. You could even assign this value to a new variable, 'total', to use later. Consider for a moment what would happen if you closed out of the browser right now, and came back later to the DataCamp site. The code that created all of those variables would be gone, right? Well that's annoying!

10. R Scripts

This is where _R scripts_ come in to play. An R script is simply a text file that holds lines of code that can be saved and run in succession whenever you want. Let's write the code that has been run so far in an R script. Now, you can run this code whenever you want. When you work through DataCamp exercises, you will write your answers in R scripts, and when you are finished, you will press the "Submit Answer" button. DataCamp's code wizards will check your answer for correctness, and you will receive personalized feedback on how you did!

11. Let's practice!

That seems like enough for now, let's get going with your first interactive exercise.