Instructions: Run any code for the questions below in the console.
Create a new object called my.num that contains any number.
# General format
my.num <- ?my.num <- 2Multiply my.num by 4.
my.num * 4## [1] 8Create a second object called my.char that contains 5 character strings.
# General format
my.char <- c("character1", "character2", ...)my.char <- c("banana", "garlic", "pickles", "hot chocolate", "7-Up") # Again,these can be anythingCombine the two objects my.num and my.char into an object called both.
both <- c(my.num, my.char)What is the length of both? Use the length() function.
length(both)## [1] 6What class is both?
class(both)## [1] "character"Divide both by 3, what happens?
both / 3## Error in both/3: non-numeric argument to binary operatorCreate a vector that contains 4 sets of the numbers 1, 2, 3, and 4.
vec_1 <- c(1, 2, 3, 4)
vec_2 <- c(vec_1, vec_1, vec_1, vec_1)Create a vector with elements 1, 10, 100, 1000 and call it z.
# General format
z <- c(...)z <- c(1, 10, 100, 1000)Multiply each number in z by 5.
z * 5## [1]    5   50  500 5000Create a vector object called int_vect that starts at 1 and goes up to 10. Use seq().
# General format
seq(from = NUMBER, to = NUMBER)int_vect <- seq(from = 1, to = 10)
int_vect##  [1]  1  2  3  4  5  6  7  8  9 10What is the length of int_vect?
length(int_vect)## [1] 10Install the tidyverse package using install.packages("tidyverse"). Load this package using library(tidyverse).
Create a vector with elements 1, 2, 3, 4, 5 and call it x.
# General format
x <- c(...)x <- c(1, 2, 3, 4, 5)Create another vector with elements 10, 20, 30, 40, 50 and call it y.
# General format
y <- c(...)y <- c(10, 20, 30, 40, 50)Determine the length of x and y. Next, add the vectors x and y together.
length(x)## [1] 5length(y)## [1] 5x + y## [1] 11 22 33 44 55# [1] 11 22 33 44 55Append the value 60 onto the vector y (hint: you can use the c() function).
# General format
y <- c(y, ...)y <- c(y, 60)Determine the length of x and y.
length(x)## [1] 5length(y)## [1] 6Add x and y together. What happens?
x + y## Warning in x + y: longer object length is not a multiple of shorter object
## length## [1] 11 22 33 44 55 61# [1] 11 22 33 44 55 61
# Warning message:
# In x + y : longer object length is not a multiple of shorter object length
# R "auto replicates" the shorter vector when the vectors aren't the same length - this is not what we want!
# In this case, it reads x as c(1, 2, 3, 4, 5, 1)!Multiply the following a and b together. How is this similar to the way R performs addition in question 2.6 ?
a <- c(1, 2, 3)
b <- c(10, 100, 1000)a * b## [1]   10  200 3000# R adds together the first element of each vector, then the second, and so on. This order also applies when multiplying!Create a vector that takes the sequence “Strongly Agree”, “Agree”, “Neutral”, “Disagree”, “Strongly Disagree” and repeats each element 10 times.
my_seq <- c("Strongly Agree", "Agree", "Neutral", "Disagree", "Strongly Disagree")
rep(my_seq, each = 10)##  [1] "Strongly Agree"    "Strongly Agree"    "Strongly Agree"   
##  [4] "Strongly Agree"    "Strongly Agree"    "Strongly Agree"   
##  [7] "Strongly Agree"    "Strongly Agree"    "Strongly Agree"   
## [10] "Strongly Agree"    "Agree"             "Agree"            
## [13] "Agree"             "Agree"             "Agree"            
## [16] "Agree"             "Agree"             "Agree"            
## [19] "Agree"             "Agree"             "Neutral"          
## [22] "Neutral"           "Neutral"           "Neutral"          
## [25] "Neutral"           "Neutral"           "Neutral"          
## [28] "Neutral"           "Neutral"           "Neutral"          
## [31] "Disagree"          "Disagree"          "Disagree"         
## [34] "Disagree"          "Disagree"          "Disagree"         
## [37] "Disagree"          "Disagree"          "Disagree"         
## [40] "Disagree"          "Strongly Disagree" "Strongly Disagree"
## [43] "Strongly Disagree" "Strongly Disagree" "Strongly Disagree"
## [46] "Strongly Disagree" "Strongly Disagree" "Strongly Disagree"
## [49] "Strongly Disagree" "Strongly Disagree"rep(c("Strongly Agree", "Agree", "Neutral", "Disagree", "Strongly Disagree"), each = 10)##  [1] "Strongly Agree"    "Strongly Agree"    "Strongly Agree"   
##  [4] "Strongly Agree"    "Strongly Agree"    "Strongly Agree"   
##  [7] "Strongly Agree"    "Strongly Agree"    "Strongly Agree"   
## [10] "Strongly Agree"    "Agree"             "Agree"            
## [13] "Agree"             "Agree"             "Agree"            
## [16] "Agree"             "Agree"             "Agree"            
## [19] "Agree"             "Agree"             "Neutral"          
## [22] "Neutral"           "Neutral"           "Neutral"          
## [25] "Neutral"           "Neutral"           "Neutral"          
## [28] "Neutral"           "Neutral"           "Neutral"          
## [31] "Disagree"          "Disagree"          "Disagree"         
## [34] "Disagree"          "Disagree"          "Disagree"         
## [37] "Disagree"          "Disagree"          "Disagree"         
## [40] "Disagree"          "Strongly Disagree" "Strongly Disagree"
## [43] "Strongly Disagree" "Strongly Disagree" "Strongly Disagree"
## [46] "Strongly Disagree" "Strongly Disagree" "Strongly Disagree"
## [49] "Strongly Disagree" "Strongly Disagree"“Strongly Agree”, “Agree”, “Neutral”, “Disagree”, “Strongly Disagree” are often responses to surveys. Create a randomly sampled vector of 30 survey responses. (hint use sample() and set the replace argument to TRUE). Store the output as my_responses. Examine the data by typing the name my_responses in the Console.
my_responses <- sample(
  x = c("Strongly Agree", "Agree", "Neutral", "Disagree", "Strongly Disagree"),
  size = 30,
  replace = TRUE
)
my_responses##  [1] "Disagree"          "Neutral"           "Neutral"          
##  [4] "Strongly Agree"    "Neutral"           "Strongly Agree"   
##  [7] "Strongly Agree"    "Disagree"          "Neutral"          
## [10] "Neutral"           "Neutral"           "Disagree"         
## [13] "Disagree"          "Neutral"           "Neutral"          
## [16] "Neutral"           "Neutral"           "Strongly Agree"   
## [19] "Strongly Agree"    "Agree"             "Neutral"          
## [22] "Strongly Disagree" "Neutral"           "Disagree"         
## [25] "Strongly Disagree" "Agree"             "Strongly Agree"   
## [28] "Agree"             "Neutral"           "Strongly Disagree"Let’s say you change your survey so participants can rank their response 1-10 (inclusive). Create a randomly sampled vector of 30 survey responses. (hint use seq() and sample() and set the replace argument to TRUE). Store the output as my_responses_2. Examine the data by typing the name my_responses_2 in the Console.
my_responses_2 <- sample(
  x = seq(from = 1, to = 10),
  size = 30,
  replace = TRUE
)