Run Your First Line of Code

Look at a real dataset, pull out a column, and make your first plot, right in your browser

Welcome. Everything on this page runs real R, right inside your browser. Nothing to install, and you cannot break anything. If a line produces an error, that is normal: read it, adjust, and run again.

The first time you click Run Code, your browser downloads R once (a few seconds). After that it is quick.

NoteThe two ideas behind almost every line of R

Writing R comes down to just two things.

An object is a named box that stores something. You make one with the arrow <-.

x <- 9      # the name x now holds the value 9

A function is a named command that takes an input and hands back a result. You use it by putting the input in parentheses.

sqrt(9)     # the sqrt function takes 9 and returns 3

You can also run a function on an object you already made. R reads the name, finds what is stored there, and uses that as the input.

sqrt(x)     # same idea: sqrt takes x (which is 9) and returns 3

That is the whole game: you store things in objects, and you act on them with functions. Everything below is practice with those two moves.

1. Look at the data

R comes with iris built in: measurements of 150 iris flowers from three species. Each flower has four measurements (sepal length and width, petal length and width) plus its species.

Click Run Code to see the first few rows.

Every row is one flower. Every column is one kind of measurement. head() is a function: it shows the top of the data so you are not flooded with all 150 rows at once.

2. Grab a single column

You pull one column out of a dataset with a dollar sign: iris$Petal.Length means “the Petal.Length column from iris.” Run this to see all 150 petal-length values.

That is a lot of numbers. It would be much easier to work with if it had a short name of its own. That is what an object is for.

3. Store the column in an object

An object is a named box that holds a value, made with the assignment arrow <-. Store the petal-length column in an object named petal_length by filling in the column name, then click Run Code.

NoteHint

The column is called Petal.Length (capital P, capital L, with a dot). It goes right after the dollar sign:

petal_length <- iris$Petal.Length
TipSolution
petal_length <- iris$Petal.Length

Now the name petal_length refers to all 150 petal-length values. Nothing prints, and that is expected: assigning a value stores it quietly.

4. Use a function to summarize it

A function takes an input and hands back a result. You already used head() above. Here is mean(), which returns the average of all the values in your object.

Run this as-is. Then click at the end of the last line, press Return, and add a line or two of your own. Some functions worth trying: median(petal_length) for the middle value, sd(petal_length) for the standard deviation, max(petal_length) and min(petal_length) for the extremes. Functions can even wrap each other, like sqrt(mean(petal_length)).

5. Turn your object into a plot

Now visualize it. hist() is a function that draws a histogram, a picture of how often each range of values shows up. Put your object inside hist(), then click Run Code.

NoteHint

Put the object you just made, petal_length, inside the parentheses:

hist(petal_length)
TipSolution
hist(petal_length)

You should see two clumps of flowers: one group with short petals and one with long petals. That gap is a real biological signal, the short-petaled setosa flowers standing apart from the other two species.

Keep playing

No grading here, and no wrong answer. Try making a different column into an object and plotting it. What does sepal length look like?

Curious how two measurements relate? This draws one flower per point, petal length against petal width:

Try this with a partner. One of you predicts what a plot will look like before running it; the other runs it. Then compare what you saw to what you expected, and swap. Saying your prediction out loud first is one of the fastest ways to learn.