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:
numerical
integer: Whole numbers with no decimals; can designate
with integer followed by letter L,
e.g. 5Ldouble: Includes both integers and real numbers
(numbers which may contain decimal values)logical: Takes on the value TRUE or
FALSE. Can explicitly set value using TRUE or
logical statement, e.g.1 < pi
## [1] TRUE
range(c(1:10))
## [1] 1 10
character: Used to store text datatypeof(): 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:
mean(x): calculates mean of a set of numbers
xmedian(x): calculates median of a set of numbers
xmode(x): calculates mode of a set of numbers
xmax(x): gets the maximum of a set of numbers
xmin(x): gets the minumum of a set of numbers
xvar(x): gets the variance of a set of numbers
xsqrt(x): calculates the square root of a numbersummary(x): gives the minumum, Q1, median, mean, Q3,
and maximum of a set of numbers xIQR(x): calculates IQR of a set of numbers
xIf we saved our data as scores, then to utilize the
function, we would enter the code as such:
mean(scores)
\[72, 85, 90, 78, 85, 91, 82, 88, 94, 75\]
Answer:
x <- c(72, 75, 78, 82, 85, 85, 88, 90, 91, 94)
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"
Answer:
max <- max(x)
Min <- min(x)
range <- max(x)-min(x)
range
## [1] 22
Answer:
var(x)
## [1] 52
sqrt(var(x))
## [1] 7.211103
Note: Be very careful with order of operations.
Answer:
variance = 52
Standard = 7.2111025
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
A: (120, 50, 130, 20)
B: (10, 400, 300)
C: (20, 40)
A. Add A and B together. What is the output? Is there any warning?
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
# 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