1 + 1 # Simple arithmetic[1] 2
1 + 1 # Simple arithmetic[1] 2
2 + 3 * 4 # Operators[1] 14
3^2 # Exponentiation[1] 9
Try these!
exp(1)[1] 2.718282
sqrt(10)[1] 3.162278
pi [1] 3.141593
Is this a common or natural logarithm?
log(3)[1] 1.098612
In R, we use the # symbol to make comments. It is best to return and continue if your comments are lengthy (rather than one long line).
# In R, we use the # symbol to make comments.
# It is best to return and continue if your
# comments are lengthy (rather than one long
# line). We will continue to learn more about
# basic R syntax throughout this introduction
# on R, including the assignment operator and
# defining variables. In RStudio the keyboard shortcut for the assignment operator is Alt - (Windows) or Option + - (Mac).
Why should we use the assignment operator <- instead of equals = ?
What are the rules for creating variable names?
Try these!
x <- 3
y <- 5
z <- 6
x * y * z
# Variable names can include variable names and periods:
This.Year <- 20255 %% 2[1] 1
20 %% 6[1] 2
42 %% 5[1] 2
R generally ignores whitespace, such as spaces and tabs, making it flexible with formatting. Note that proper use of whitespace improves code readability. For example:
x<-10
x <- 10Should I use print statements?
You may use print statements for your reports, but for your own coding, we generally do not use print statements as they can become time consuming and may clutter the source code.
An alternative is the cat() function which can be used to concatenate and print text. Compare the following outputs:
x <- 10
print(x)[1] 10
cat(x)10
The cat() function more formally:
x <- 10
cat("The value of x is:", x," \nThis is the next line.")The value of x is: 10
This is the next line.
The slash and n tells R to move down to a new line.
#getwd()To set your working directory:
#setwd("C:/Users/llmur/Desktop/ADS 2288F Programming Skills/_Lectures/_Slides/R Programming I Introduction")Run getwd() again to verify!
Alternatively, you can go to “Session” –> “Set Working Directory.”
Why is it recommended to use <- instead of = for assignment in R?
When using a function like exp() or sqrt(), how can you find out what other arguments it accepts?
How does being case-sensitive influence how you choose your variable names in R?
Why might clean formatting and consistent whitespace matter, even if R ignores it when executing the code?
In what situations might you prefer cat() over print()? What are the advantages or disadvantages?
Why is it important to understand your working directory in RStudio?
How can setting your working directory incorrectly affect your workflow?
What are some common mistakes that cause error messages in R? How would you go about fixing them?
What is R used for, and how does it compare to Python?
What is a built-in function in R? Give two examples.
Which of the following are valid variable names in R?
my_var
2value
.score
%dataCreate a new variable a and assign it the result of 10 %% 3. What does this operator do?
You want to check where R is currently reading/writing files and then change it to your “Documents” folder. What two lines of code do you use?
You want to create a variable called summary but get strange errors when running summary functions. Why?
Why is whitespace important in R scripts?
What is the problem with this code?
score <- 80
passing <- 70
score => passingWrite R code that stores your name in a variable and then prints a message like “Hello, your_name!” using cat().
Compare these two statements. How are they different?
print("Result:", 6 + 2)
cat("Result:", 6 + 2)print("The result is:" 4 + 4)print("The result is:", 10 + 5) You created a variable z <- 6. How can you test if R recognizes it without printing it manually?
Fatima runs the following code:
x <- 10
x == 10She sees [1] TRUE as the output. Why doesn’t this assign anything to x?
5 + "apple"What error does R return, and why?
a <- 4
b <- 2
a > bWhat does it return, and how can he interpret it?
x <- "5"
x + 2x <- 5
y <- "5"
x == y Using the the variables a <- 3 and b <- 4, create a code that will display “Sum: 7 Product: 12”. HINT: use a+b and a*b
Write a line that adds 3 and 4, divides the result by 2, and prints it.
Create a line of code that prints the result of 5 squared and states what operation you did.
Write a line of code that calculates the natural log of 100 and rounds the result to 2 decimal places. HINT: Use the built-in function round()
Create a line of code that prints the value of pi with 3 decimal places.
What happens if you run sqrt(-1) in R?
What do “\n” and “\t” do in R? Give an example.
What code produces this output? Indented Newline
Indented
Newline cat("Good morning!")
cat("Welcome back!") Jack be nimble,
Jack be quick,
Jack jumped over
The candlestick.cat("Result:\n", 3^2, "\n", "Squared")But the output isn’t formatted like he expected. Rewrite the line so it prints:
Result:
9 squareda<-10
b<-3
c=a%%b
cat(c)while(TRUE) {
print("Oops")
}x=5
Y <- 2
print("Sum is:", x + y)