Exercise 1. install packages

Use the package panel in RStudio to install the following packages: - “psych” - “lsr” - “car” packages

Exercise 2. Remove all workspace variables

Run the code chunk to remove the variables in your workspace.

rm(list = ls())

Exercise 3. Create variables

Below each comment in the code chunk, write code that does what the comment describes.

# the code below creates a "seeker" variable that contains the value 3.1415
seeker <- 3.1415 

# create a "lover" variable that contains the value 2.7183
lover <- 2.7183
# multiply "seeker" and "lover" to create a third variable called "keeper"
keeper <- seeker * lover
# print out the value of "keeper"
print(keeper)
## [1] 8.539539
# use the objects function to print the names of the variables in your workspace
objects()
## [1] "keeper" "lover"  "seeker"

Exercise 4. Load package & use who()

Run the code chunk.

library(lsr)
who()
##    -- Name --   -- Class --   -- Size --
##    keeper       numeric       1         
##    lover        numeric       1         
##    seeker       numeric       1

Exercise 5. Store text in variable

Create a variable called myName that stores your first name. For example, I would assign the value “Brandi” to the myName variable. Print out the value of myName. Then use the who() function to show info about current workspace variables.

# create a "myName" variable, with your first name as it assigned value
myName <- "Eli"
# print the value of "myName"  
myName
## [1] "Eli"
# use the who function below to show the name, class, and size for each variable in the workspace
who()
##    -- Name --   -- Class --   -- Size --
##    keeper       numeric       1         
##    lover        numeric       1         
##    myName       character     1         
##    seeker       numeric       1

Exercise 6. Removing variables

Remove the variables created in #3 above (seeker, lover, and keeper) and then use the who() function to check whether those variables are still in your workspace.

# remove the seeker, lover, and keeper variables
rm(seeker, lover, keeper)
# use the who function below to check whether the variables are removed
who()
##    -- Name --   -- Class --   -- Size --
##    myName       character     1

Exercise 7. Navigating the file system

Run the code chunk to get your working directory.

getwd()
## [1] "/Users/elijah.d.johnson/Downloads"

Exercise 8. Loading files

Edit the code chunk so that it loads booksales.Rdata from wherever you saved the “data” folder you downloaded for HW1. Then use the who() to see info about the variables you added to your workspace.

# edit code below to load "booksales.Rdata" from your "data" folder 
load("~/Downloads/.RData")
# list workspace variables & info about them
who()  
##    -- Name --       -- Class --   -- Size --
##    apples           numeric        1        
##    february.sales   numeric        1        
##    months           character     12        
##    months.sold      character      4        
##    myName           character      1        
##    price            numeric        1        
##    sales.by.month   numeric       12        
##    summer           character      3        
##    totalCost        numeric        1        
##    winter           character      3

Exercise 9. Listing files in directory

Run the code chunk to see all the files within your working directory.

Exercise 10. Print() function

Use the print() function to see the values for the months and sales.by.month variables you loaded from booksales.Rdata. Both should contain 12 values, corresponding to the the months of the year.

# print months
print(months)
##  [1] "January"   "February"  "March"     "April"     "May"       "June"     
##  [7] "July"      "August"    "September" "October"   "November"  "December"
# print sales.by.month
print(sales.by.month)
##  [1]   0 100 200  50  25   0   0   0   0   0   0   0

Exercise 11. Assigning names to vector elements

Run the code chunk, which uses the months vector to assign the names of the months to sales.by.month.

#assign the contents of "months" as names for the elements of "sales.by.month"
names(sales.by.month) <- months
#print out the result, which should now include the assigned names
print(sales.by.month)
##   January  February     March     April       May      June      July    August 
##         0       100       200        50        25         0         0         0 
## September   October  November  December 
##         0         0         0         0

Exercise 12. Indexing

Edit the code chunk so that it prints only the February sales from the sales.by.month vector:

# print February sales (hint: the 2nd element of sales.by.month)
print(sales.by.month)[february.sales]
##   January  February     March     April       May      June      July    August 
##         0       100       200        50        25         0         0         0 
## September   October  November  December 
##         0         0         0         0
## <NA> 
##   NA

Exercise 13. Variable classes

Correct the code so that it multiplies 5 times 4.

# edit the code below to assign numeric rather than text values
x <- 5
y <- 4
# multiply x times y
x*y
## [1] 20
# check the class of the variables
class(x)
## [1] "numeric"
class(y)
## [1] "numeric"
# check the class of any.sales.this.month (from booksales.Rdata)
class(sales.by.month)["September"]
## [1] NA

Exercise 14. Variable classes

Check the class of the any.sales.this.month that you loaded into your workspace from the booksales.Rdata file.

# check the class of the variable
class(any.sales.this.month)
## Error: object 'any.sales.this.month' not found

Exercise 15. Introducing factors

Create a variable called group that is assigned the following values: 1,1,1,2,2,2,3,3,3,4,4,4.
Then use class() to check the class of the group variable.

# assign the values above to a variable called "group"
group <- c(1,1,1,2,2,2,3,3,3,4,4,4)
# check the class of the "group" variable
class(group)
## [1] "numeric"
# the code below prints group
print(group)
##  [1] 1 1 1 2 2 2 3 3 3 4 4 4

Exercise 16. Treating variables as factors

Now use the as.factor() function to tell R to treat the variable group as a factor. Then use the class() function to check the class of the group variable again.

# make the "group" variable a factor
group<- as.factor(group)
# check the class of the "group" variable
class(group)
## [1] "factor"

Exercise 17. Labeling factor levels

Run the code chunk, which creates a gender variable that includes values for each of 12 participants. Each value corresponds to a particular gender - in this example there are 4 gender options: 1=“female”, 2=“male”, 3=“nonbinary”, 4=“other”. (Feel free to edit the code below to include other options, as long as there are at least 2 different levels. Or just run the code as it is.)

# create a "gender" variable that stores gender for 12 participants
gender <- c(4,2,1,3,1,2,4,3,2,3,4,1)
# tell R to treat "gender" as a factor
gender <- as.factor(gender)
# assign meaningful labels that for each of the 4 levels of gender 
# (1: "female", 2: "male", 3: "nonbinary", 4: "other")
levels(gender) <- c("f", "m", "nb", "other")
#print gender
gender
##  [1] other m     f     nb    f     m     other nb    m     nb    other f    
## Levels: f m nb other

Exercise 18. Labeling factor levels

Assign the following labels to the 4 different group levels:
1: “group 1”, 2: “group 2”, 3: “group 3”, 4: “group 4”.

# assign meaningful labels that for each of the 4 levels of group 
levels(group)<-c("group 1", "group 2", "group 3", "group 4")
# the code below prints "group" 
group
##  [1] group 1 group 1 group 1 group 2 group 2 group 2 group 3 group 3 group 3
## [10] group 4 group 4 group 4
## Levels: group 1 group 2 group 3 group 4

Exercise 19. Data frames

Create two variables: age and score. Select 12 values to assign to each variable, with age values ranging between 18 and 40, and score values ranging from 0 to 20.

# Create an "age" variable and assign it 12 values (1 for each of 12 participants).
age<-c(19,21,37,18,19,38,18,19,23,20,30,40)
# Create a "score" variable and assign it 12 values, ranging between 0 and 20.
score<-c(0,3,5,8,10,7,20,18,13,4,11,12)
# the code below shows info about workspace variables, including age & score
who()
##    -- Name --       -- Class --   -- Size --
##    age              numeric       12        
##    apples           numeric        1        
##    february.sales   numeric        1        
##    gender           factor        12        
##    group            factor        12        
##    months           character     12        
##    months.sold      character      4        
##    myName           character      1        
##    price            numeric        1        
##    sales.by.month   numeric       12        
##    score            numeric       12        
##    summer           character      3        
##    totalCost        numeric        1        
##    winter           character      3        
##    x                numeric        1        
##    y                numeric        1

Exercise 20. Data frames

Create a dataframe called df that includes the following variables: age, gender, group, and score.

# create the dataframe below
df<-data.frame(age=age, gender=gender, group=group, score=score)
# the code below prints the dataframe
df
##    age gender   group score
## 1   19  other group 1     0
## 2   21      m group 1     3
## 3   37      f group 1     5
## 4   18     nb group 2     8
## 5   19      f group 2    10
## 6   38      m group 2     7
## 7   18  other group 3    20
## 8   19     nb group 3    18
## 9   23      m group 3    13
## 10  20     nb group 4     4
## 11  30  other group 4    11
## 12  40      f group 4    12

Exercise 21. Pulling contents out of dataframes

Pull out scores from the df dataframe you created.

# pull out the "scores" variable from df
df$score
##  [1]  0  3  5  8 10  7 20 18 13  4 11 12

Exercise 22. Getting info about a dataframe

Use the names() function to get the names of all the variables stored in df.

# get the names of the variables stored in df
names(df)
## [1] "age"    "gender" "group"  "score"

Exercise 23. Lists

Use the list() function to create a list with your first name as the name of the list. The list should the following 3 variables and assign each the values described below: - age: your current age - hasPets: “TRUE”/“FALSE”, depending on whether you have pets - hobbies: 2 hobbies you enjoy doing

# edit the code below to create your list
Eli <- list (age = 22, hasPets = TRUE, hobbies = c("video games", "hiking"))
# edit code below to print your list
Eli
## $age
## [1] 22
## 
## $hasPets
## [1] TRUE
## 
## $hobbies
## [1] "video games" "hiking"

Exercise 24. Lists

Pick another variable to add to the list you just created, maybe your favorite show/book/band. Or if you can’t pick just one favorite, you can include multiple (like we did for hobbies). For example, I might add a variable called favorite.shows and set the values to be “Buffy the Vampire Slayer”, “Patriot”, “Broad City”, and “Fleabag”. Or I might add a favorite.bands variable, with values such as “Ron Gallo”, “La Luz”, “Kikagaku Moyo”, “Pink Floyd”, “Mazzy Star”, and “Jackie Shane”.

# add the new variable to your dataframe
Eli$favorite.shows <- c("Dexter", "Blacklist", "Suits", "Ted Lasso")
# print the result of your dataframe 
Eli
## $age
## [1] 22
## 
## $hasPets
## [1] TRUE
## 
## $hobbies
## [1] "video games" "hiking"     
## 
## $favorite.shows
## [1] "Dexter"    "Blacklist" "Suits"     "Ted Lasso"

Exercise 25. Getting help

Run the code chunk to see the help file for levels(). When you run it, the help panel should show the documentation for the function.

# get help documentation for levels function
?levels

Exercise 26. Short answer question

In #25 you looked at help documentation for the levels() function. What are the 2 arguments for this function? Type your answer below: x
an object, for example a factor.

value
a valid value for levels(x). For the default method, NULL or a character vector. For the factor method, a vector of character strings with length at least the number of levels of x, or a named list specifying how to rename the levels.