Brigham Young University**We aren't endorsed by this school
Course
MSB 325
Subject
Statistics
Date
Dec 16, 2024
Pages
6
Uploaded by BaronAardvarkMaster926
# Lesson 6 Assignment# Your assignment is to write the commands instructed in the comments below. To run your# commands, simply hit Ctrl+Enter (command+return on a MAC) when the cursor is on that # command line. You can also type commands directly into the Console below, but you must# save them in this file for your assignment.library(dplyr)library(readxl)nps <- read_excel("NewProductSurvey.xlsx")nps <- data.frame(nps)#1. Create four variables, *var1*, *var2*, *var3*, and *var4*, with the values of 3, 10, # 24, and 214, respectively.var1 <- 3var2 <- 10var3 <- 24var4 <- 214#2. Create four *if()* statements. Each *if()* statement should check whether *var1*, # *var2*, *var3*, or *var4*, respectively, is greater than 10. If the variable is greater # than 10, a variable var1g10 (or var2g10, var3g10, var4g10, etc.) should be assigned TRUE.# If not, nothing should happen. if(var1 > 10) { var1g10 <- TRUE}if(var2 > 10) {var2g10 <- TRUE}if(var3 > 10) { var3g10 <- TRUE}if(var4 > 10) { var4g10 <- TRUE}#3. Starting with the *if()* statements from question 2, make the following alteration: if # the variable is not greater than 10, the variable var1g10 (or var2g10, var3g10, var4g10,# etc.) should be assigned FALSE. if(var1 > 10) {var1g10 <- TRUE} else{var1g10 <- FALSE}
if(var2 > 10) {var2g10 <- TRUE} else{var2g10 <- FALSE}if(var3 > 10) {var3g10 <- TRUE} else{var3g10 <- FALSE}if(var4 > 10) {var4g10 <- TRUE} else{var4g10 <- FALSE}#4. Make another adjustment to the *if()* statements from question 3. After checking whether # the variable is greater than 10, the statement should then check whether the variable # equals 10. If the variable equals 10, a new variable var1ge10 (or var2ge10, var3ge10, # var4ge10, etc.) should be assigned TRUE. If the variable is not greater than or equal # to 10, it should continue to assign FALSE to var1g10 (or var2g10, var3g10, var4g10, etc.) if(var1 > 10){var1g10 <- TRUE} else if(var1 == 10){var1ge10 <- TRUE} else{var1g10 <- FALSE}if(var2 > 10){var2g10 <- TRUE} else if(var2 == 10){var2ge10 <- TRUE} else{var2g10 <- FALSE}if(var3 > 10){var3g10 <- TRUE} else if(var3 == 10){var3ge10 <- TRUE} else{var3g10 <- FALSE}if(var4 > 10){var4g10 <- TRUE
} else if(var4 == 10){var4ge10 <- TRUE} else{var4g10 <- FALSE}## Practice with *for()* loops.#5. Create a variable called *vec1* that is a vector with four values: 3, 10, 24, 214. # Create a variable called *vec2* that contains these four values: 0, 0, 0, 0.vec1 <- c(3, 10, 24, 214)vec2 <- c(0, 0, 0, 0)#6. Use a *for()* loop to create a variable called *vec2* that contains the values: 5, # 12, 26, 216. (Loop from 1 to 4, replacing the *vec2* value with the corresponding *vec1*# value plus 2.) for(i in 1:4) {vec2[i] <- vec1[i] + 2}#7. Add *vec1* and *vec2* and assign the resulting addition to vec3.vec3 <- vec1 + vec2## Practice using *for()* loops with embedded *if()* statements.#8. Create a vector of length 4 called vec1check. Assign the letter 'a' to each entry.# Create a *for()* loop that checks the values of *vec1* and assigns "Greater than 10", # "Equals 10", or "Less than 10" to the corresponding location in vec1check, depending # on the value of the *vec1* entry. (In other words, you're using part of the *if()* # statement from question 4. Remember, we're working with vec1 and vec1check now, not# var1 or var1g10 anymore.) vec1check <- c("a", "a", "a", "a")for (i in 1:length(vec1)) {if (vec1[i] > 10) {vec1check[i] <- "Greater than 10"} else if (vec1[i] == 10) {vec1check[i] <- "Equals 10"} else {vec1check[i] <- "Less than 10"}}# For the remainder of the assignment, we will be working with the nps data frame# (nps for new product survey). This data comes from a marketing research survey of # consumers who were each asked to evaluate a new product concept on five dimensions. # But the survey respondents were randomly assigned to two different groups.
# Participants with a Q1 value of 1 watched a video about the new product. Participants# with a Q2 value of 1 tested the product before completing their evaluations. Because# of this division, Q1 participants' evaluations are in columns Q3 through Q7, while# Q2 participants' evaluations are in columns Q8 through Q12.#9. Create variable Q13 in nps. It is a placeholder variable. Fill it with 0s.nps$Q13 <- rep(0, nrow(nps))#10. Now we want to fill in the Q13 values. Q3 and Q8 contain answers to the same # question, but they were recorded in separate columns because of the way the survey# was structured in Qualtrics. Q13 should have the Q3 value for participants in# the video condition (Q1 == 1). It should have the Q8 value for participants in the# testing condition (Q2 == 1). Use a 'for' loop with an embedded 'if' statement.for (i in 1:nrow(nps)) {if (nps$Q1[i] == 1) {nps$Q13[i] <- nps$Q3[i] # Assign Q3 value for video conditionprint(paste("Q13[", i, "] assigned Q3 value:", nps$Q3[i]))} else if (nps$Q2[i] == 1) {nps$Q13[i] <- nps$Q8[i] # Assign Q8 value for testing conditionprint(paste("Q13[", i, "] assigned Q8 value:", nps$Q8[i]))}}#11. The same thing is true of Q4 to Q7 and Q9 to Q12. Q4 and Q9 are the same question;# Q5 and Q10 are the same question; Q6 and Q11 are the same question; and Q7 and Q12 are# the same question. Create columns Q14 through Q17 and fill them with the appropriate# values.nps$Q14 <- rep(0, nrow(nps))nps$Q15 <- rep(0, nrow(nps))nps$Q16 <- rep(0, nrow(nps))nps$Q17 <- rep(0, nrow(nps))for (i in 1:nrow(nps)) {if (nps$Q1[i] == 1) {nps$Q14[i] <- nps$Q4[i]nps$Q15[i] <- nps$Q5[i]nps$Q16[i] <- nps$Q6[i]nps$Q17[i] <- nps$Q7[i]print(paste("Assigned Q14-Q17 values for row", i, "from Q4-Q7"))} else if (nps$Q2[i] == 1) {nps$Q14[i] <- nps$Q9[i]nps$Q15[i] <- nps$Q10[i]nps$Q16[i] <- nps$Q11[i]nps$Q17[i] <- nps$Q12[i]
print(paste("Assigned Q14-Q17 values for row", i, "from Q9-Q12"))}}#12. Q13 through Q17 now contain the answers to the five questions about participants'# evaluations of the new product. But if you look at the data, you'll notice that the# responses are highly correlated. If someone evaluates the product highly in the first# question, they tend to rate it highly on the other four questions. Create column Q18# that contains the average response for each participant.nps$Q18 <- rowMeans(nps[, c("Q13", "Q14", "Q15", "Q16", "Q17")])print("Q18 has been calculated as the average of Q13-Q17")#13. We want to know whether the video or testing the product resulted in higher# evaluations from participants. Create a report called npi that shows the mean# evaluation (from Q18) for participants in each condition. (Use tidyverse verbs.)# The npi report should only have two columns.npi <- nps %>%mutate(condition = ifelse(Q1 == 1, "Video", "Testing")) %>%group_by(condition) %>%summarize(mean_evaluation = mean(Q18, na.rm = TRUE))print(npi)#14. Which condition (video or testing) results in higher evaluations?# *testing*#15. Do you think this difference in evaluations is real or just random noise? (This# question is meant to spark your thinking. Either answer will be marked correct.)# *I think it is real*# Doing a 'for' loop with embedded 'if' statements can be useful, as you just saw. But# there is a more efficient way to conduct those analyses. The 'ifelse' command lets# you evaluate an entire vector of values at once. To learn about the 'ifelse' command,# type the following command into the console: help("ifelse").# The 'ifelse' command has three inputs. First is the condition you're testing. The# second is the result you want when the test is TRUE. The third is the result you want# when the test is FALSE.#16. Use the 'ifelse' command to create variable Q13b, which should be the same as # Q13. That is, test whether Q1 is 1 or 0, and fill in Q13b with either Q3 or Q8,# depending on the result. You do not need to do a 'for' loop because 'ifelse' can# evaluate the entire vector at once.nps$Q13b <- ifelse(nps$Q1 == 1, nps$Q3, nps$Q8)print("Q13b created using ifelse command based on Q1 and Q8")
#17. Use the 'ifelse' command to create variables Q14b through Q17b, which should# have the same values as Q14 through Q17.nps$Q14b <- ifelse(nps$Q1 == 1, nps$Q4, nps$Q9)nps$Q15b <- ifelse(nps$Q1 == 1, nps$Q5, nps$Q10)nps$Q16b <- ifelse(nps$Q1 == 1, nps$Q6, nps$Q11)nps$Q17b <- ifelse(nps$Q1 == 1, nps$Q7, nps$Q12)# For this simple case, the 'ifelse' command is a more efficient method than using # a 'for' loop with embedded 'if' statements, but they both work.