In lecture, we introduced the idea of a variable. R also has these kinds of variables as well. While they fall into categorical and numerical, there are more specific classes commonly used in coding:

1 < pi
## [1] TRUE
range(c(1:10))
## [1]  1 10

typeof(): The typeof() function in R tells you the type of data stored in a variable. It gives you the fundamental data type, like numeric, character, or logical. For example, typeof(5) returns “double” because 5 is a numeric value, and typeof("hello") returns “character” because “hello” is a string of characters.

class(): provides information about the class or type of object. It gives you a more specific classification of the object, which can be useful for understanding its behavior and how it interacts with other functions. For example, class(5) returns “numeric” because 5 is an object of the numeric class, and class("hello") returns “character” because “hello” is an object of the character class.


We will now be recreating the exercises we conducted in class.

Recall how to read in a set of data:

x <- c(1, 2, 3)

Some potentially helpful functions:

If we saved our data as scores, then to utilize the function, we would enter the code as such:

mean(scores)
  1. Read in the following data

\[72, 85, 90, 78, 85, 91, 82, 88, 94, 75\]

Answer:

 x <- c(72, 75, 78, 82, 85, 85, 88, 90, 91, 94)
  1. Calculate the mean, median, and mode of the data set.

Answer:

mean(x)
## [1] 84
median(x) 
## [1] 85
mode(x) # mode appears as numerical but it is 85 since it occurs the most
## [1] "numeric"
  1. Calculate the range of the data set by using the maximum and minumum of the dataset.

Answer:

max <- max(x)
Min <- min(x) 
range <- max(x)-min(x)
range
## [1] 22
  1. Calculate the variance and standard deviation using the built in R function.

Answer:

 var(x) 
## [1] 52
sqrt(var(x)) 
## [1] 7.211103
  1. Now calculate the variance and standard deviation by hand. Confirm that your answer is similar to the one above.

Note: Be very careful with order of operations.

Answer:

variance = 52
Standard =  7.2111025
  1. Calculate the IQR of the data by first finding Q1 and Q3. You may use the built in function to double check, but you must include the values for Q1 and Q3.

Answer:

summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    72.0    79.0    85.0    84.0    89.5    94.0
Q1 = 79
Q3 = 89.5
IQR = Q3-Q1 
IQR
## [1] 10.5

  1. You are given three vectors:

Answer:

A <- c(120, 50, 130, 20)
B <- c( 10, 400, 300)
addition = NA
# received error message: longer object length is not a multiple of shorter object length

TYPE RESPONSE HERE

B. Add A and C together. What is the output? Is there any warning?

Answer:

A <- c(120, 50, 130, 20)
C <- c(20, 40)
addition = A+C
# there was no error message in adding A and C

TYPE RESPONSE HERE

  1. In the following code, 100 numbers are randomly generated.
# DO NOT CHANGE THIS CODE
set.seed(4)

prob8 <- rnorm(100)

What is the 11th value in the vector prob8?

Answer:

prob8[11]
## [1] 0.5666045