Lesson 2 of 16

Variables and Types

Variables

In R, you assign values to variables using the <- operator:

x <- 42
name <- "Alice"
is_active <- TRUE

You can also use = for assignment, but <- is the conventional R style.

Basic Types

R has several fundamental types:

TypeExampleDescription
numeric3.14Numbers (both integers and doubles)
character"hello"Strings (text)
logicalTRUE, FALSEBoolean values
integer42LExplicit integer (note the L suffix)

Checking Types

Use class() to check the type of a value:

cat(class(42), "\n")       # "numeric"
cat(class("hello"), "\n")  # "character"
cat(class(TRUE), "\n")     # "logical"

String Concatenation

Use paste() or paste0() to combine strings:

name <- "World"
cat(paste("Hello,", name), "\n")   # "Hello, World" (space separator)
cat(paste0("Hello, ", name), "\n") # "Hello, World" (no separator)

paste() adds a space between arguments by default. paste0() concatenates without any separator.

Your Task

Create three variables: name (set to "R"), year (set to 1993), and is_free (set to TRUE). Print each on its own line using cat().

R runtime loading...
Loading...
Click "Run" to execute your code.