Run Your First Line of Code

Make your own objects, explore real data, and plot your first histogram — all 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. Make three objects of your own

Fill in the editor with your own object names and values. Make two numbers and one sentence (text in quotes).

Object names cannot begin with a digit or contain spaces. R is case-sensitive: MyVar and myvar are different names.

NoteHint

Pick any names you like (for example score_a, score_b, motto) and any values. Numbers go in without quotes; sentences need quotes:

score_a <- 42
score_b <- 7
motto <- "Science is cool."
TipSolution

Any valid answer works as long as the names and values are yours. For example:

score_a <- 42
score_b <- 7
motto <- "Science is cool."

Nothing prints when you assign — that is expected. The values are stored quietly until you use them.

2. Add your two numbers

Use the names you chose in Part 1. Add your two number objects together with +, then click Run Code.

NoteHint

Type the names of your two number objects with a plus sign between them. If you named them score_a and score_b:

score_a + score_b
TipSolution

Use whatever names you picked in Part 1:

score_a + score_b

R looks up each name, finds the numbers stored there, and returns their sum.

3. Break it on purpose

Now try adding both numbers and your sentence object in one line. Run it and read the error message.

NoteHint

Use the same three object names from Part 1, joined with +:

score_a + score_b + motto
TipSolution
score_a + score_b + motto

You should see an error like non-numeric argument to binary operator. That is the point — R knows how to add numbers, but it will not silently combine a number with a sentence.

+ is defined for numbers: R adds the values. A sentence in quotes is character data, not a number. When R hits number + "text", it stops and tells you the second argument is not numeric. Errors are information, not failure — read the message and adjust.

5. Load a package, meet the penguins

Real datasets often live in packages — bundles of code and data other people wrote. library() switches a package on. Run this to load palmerpenguins and peek at the data.

Every row is one penguin. Every column is one measurement or label. head() is a function that shows the first few rows so you are not flooded with all 344 at once.

6. Inspect the structure

str() is a function that summarizes a dataset: how many rows, how many columns, and the type of each column. Run it on penguins.

From the str() output, answer these in your head (or jot them down):

  1. How many rows (penguins) are in the dataset?
  2. How many columns (variables) does it have?
  3. What type is the species column?

Answers: 344 rows; 8 columns; species is a Factor (R’s way of storing categories like Adelie, Chinstrap, and Gentoo).

7. Pull a column into an object

You pull one column out of a dataset with a dollar sign: penguins$body_mass_g means “the body_mass_g column from penguins.” Store it in an object named body_mass by typing the column name after the $.

NoteHint

The column is called body_mass_g. It goes right after the dollar sign:

body_mass <- penguins$body_mass_g
TipSolution
body_mass <- penguins$body_mass_g

Now body_mass holds all 344 body-mass values (grams). Some are NA, meaning that penguin was never weighed — we will deal with that next.

8. Summarize with mean() — and the NA surprise

mean() returns the average. Run this as-is and notice what comes back.

NA means “missing.” Two penguins in this dataset were never weighed, so R refuses to average around a hole unless you tell it otherwise. Add na.rm = TRUE (read: “remove NAs”) to skip missing values.

NoteHint

Type TRUE after na.rm =:

mean(body_mass, na.rm = TRUE)
TipSolution
mean(body_mass, na.rm = TRUE)

You should see a real number — the average body mass in grams, ignoring the two missing penguins.

9. Turn your object into a labeled plot

hist() draws a histogram — a picture of how often each range of values shows up. Add a title and axis label inside the quotes.

NoteHint

Any descriptive title and label work. For example:

hist(body_mass,
     main = "Penguin body mass",
     xlab = "g")
TipSolution
hist(body_mass,
     main = "Penguin body mass",
     xlab = "g")

Most penguins cluster around 3,000–4,500 g, with a long tail toward heavier birds.

10. Recap — you already wrote the whole script

If you ran every step above in order, you already executed the core of Coding Assignment 1. Here is the full five-line script from the assignment, using penguin_data instead of data (both work; data is also a built-in R function name, so many people avoid it):

library(palmerpenguins)
penguin_data <- penguins
str(penguin_data)
mean(penguin_data$body_mass_g, na.rm = TRUE)
hist(penguin_data$body_mass_g, main = "Penguin body mass", xlab = "g")
  1. library(palmerpenguins) — Loads the package so the penguins dataset becomes available.
  2. penguin_data <- penguins — Copies the dataset into a shorter object name you can reuse.
  3. str(penguin_data) — Prints the structure: 344 rows, 8 columns, and the type of each column.
  4. mean(..., na.rm = TRUE) — Computes the average body mass in grams, skipping penguins with missing weights.
  5. hist(...) — Draws a histogram of body mass with a title and x-axis label.

If you can explain each line in your own words, you are ready for the graded assignment on Canvas.

Keep playing

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

Curious how two measurements relate? This draws one penguin per point, flipper length against body mass:

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.