Basic syntax

1 Basic Syntax

Keep the intro.R script open, but now type directly into the R console below.

3 + 5
[1] 8

What happens to the output?

1.1 R objects

Assign an R object called juice to be equal to 4+4. What is the output when you type juice?

juice <- 4 + 4

# juice

Can you describe what you think x indicates below?

x <- c(1,7,9)
x[2]
[1] 7

The notation c() in R stands for combine. We used this a lot in R because we are often combining data and comparing it with another dataset.

What do you think the R object fruit is?

apples <- 5
oranges <- 6

fruit <- apples + oranges

2 Operators

Symbols in R are used in arguments, logical statements, assignments, and more. We will use many of them, but the common ones

# <- vs. =
a = 3 + 4
a <- 3 + 4

3 Errors

# Errors and improper R object naming
# 6389/

# 5-6

# 5.6 <- fruit

3.1 Activity

There are several ways to view or look at an R object. Can you figure out what they are?

3.2 Activity

Calculate the volume of a sphere, where the diameter is 20 meters.

3.3 A note on documenting code

Make sure current you keeps track of everything. Future you will Thank past you.

Also use the pound symbol or hashtag mark, “#” to comment out code.

# This code adds the values x and y, creating an R object called 'answer'

answer <- x + y

x <- 5
y <- x^2

## We can set x equal a specific value to define x, y, and answer.