R starts here!

R is a powerful programming language that excels at data analysis and statistics. But really, it can do almost anything!

But let’s start simple: Out-of-the-box R can do everything you’d expect a calculator to do. Here’s a code cell where you can put in any R code you want.

But for now, just add two numbers together and press ▶ Run Code.

Friends of +, like -, /, and * work as expected. R also includes a large number of mathematical functions, like log(), exp(), and sqrt(). To call a function you write its name followed by the argument between parentheses, like log(2.72) or sin(1.57).

Calculate the square root of 1764 using sqrt().

Functions can have more than one argument, which, are, then, separated, by, commas. For example, max(1, 3, 5, 4, 2) returns the highest of it’s arguments: 5. Another function that can take many arguments is sum().

Use sum() to sum up several large numbers, I don’t care which, but they should total more than 1,000,000!

R can work with more than numbers and there are many other types of data, or data types, in R. A piece of text, often called a string, is written by putting the text “into quotes”.

Try writing a string, any string, in the code cell below. I’ll repeat it three times.

Like with numbers, you can also apply functions to strings. For example, tolower() turns all characters into lower case.

We’re in a library. Use tolower() on the string to get the upset person to calm down.

And, of course, there are functions that take both numbers and strings as arguments. But the order of the arguments is important! For example, for rep() the first argument is the value to repeat, and the second argument is the number of repetitions.

Repeat a moderately dirty word a 100 times using rep().

It might not be obvious, but the result above is actually a list of 100 strings. That’s why there are [ ] with numbers showing the index of the right-most elements. In R, lists where all elements are of the same data type are called vectors (to confuse you, there’s also something else called a list in R, but that’s for another time).

I’ve gone ahead and created a numeric vector for you. It’s called temp and contains the max C° temperature in Hyderabad, India for each day in June 2023 (source). In R you can print out a representation of whatever you want to look at by simply writing its name.

Take a look at the data in temp.

But, there are 30 days in June, right? Are there really 30 temperatures in temp?

Use the length() function to count the number of elements in temp.

And what was the highest temperature in Hyderabad in June 2023?

Use max() to get the highest value in temp.

But it wasn’t 42.4 C° every day, I hope. What was the mean temperature?

Use, you guessed it, mean() to get the mean/average of temp

As you’ve seen, there are many things you can do, once you’ve gotten some data into R. One thing that’s always nice to do with new data is to plot it!

Use plot() to make a simple time-series plot of temp.

R is great for making beautiful plots, but that usually takes a bit more effort. But, as the last exercise in this chapter, let’s spiff up this plot, just a little bit!

Many functions in R can be given optional arguments, and plot() has many that change the appearance of the plot. Arguments to a function can also be given in the form the_argument_name = "the value", and then the argument order doesn’t matter anymore.

For the plot arguments below, replace ______ between the "" to your liking.

Great work! Now on to 👉Chapter 2: Vectors and variables👈