Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

 

HW2: More Data Objects

Stat 33B, Fall 2021

 

Introduction

The purpose of this assignment is to work with various data objects in R (e.g. vectors, lists, data.frames).

 

General Instructions

• Write your narrative and code in an Rmd (R markdown) file.

•  Name this file as hw2-first-last.Rmd, where first and last are your first and last names (e.g. hw2-gaston-sanchez.Rmd).

• Please do not use code chunk options such as: echo  =  FALSE, eval  =  FALSE, results =  'hide '. All chunks must be visible and evaluated.

 

 

 

1) Technical Questions about data frames

Answer the following questions (using your own words). You do NOT need to include any commands, although you may have to run some commands to understand what’s going on.

a. What happens when you use the dollar $ operator on a data frame, attempting to refer to the name of a column that does not exist? For example: dat$xyz where there is no column named xyz.

 

b. Which of the following commands fails to return the vector mpg which is a column in the built-in data rfame mtcars? And what is the reason that makes such command to fail?:

1. mtcars$mpg

2. mtcars[  ,1]

3. mtcars[[1]]

4. mtcars[  ,mpg]

5. mtcars[["mpg"]]

6. mtcars$"mpg"

7. mtcars[  ,"mpg"]

 

c. Can you include an R list as a “column” of a data frame? YES or NO, and why.


d. The object mtcars is a data.frame.  What happens when you apply, separately, the functions as.list() and unclass() to a data frame?  e.g. as.list(mtcars), and unclass(mtcars). In other words, when applied to a data.frame, what do as.list() and unclass() have in common, and what is different?

e. Consider the command: abc  <-  as.list(mtcars). What function(s) can you use to convert the object abc into a data frame?

 

2) Camping Tents

Use the data table in camping-tents.csv (the same data used in lab-4). You can find the associated file in the same folder containing this pdf of instructions.


#  import  using   'read.csv() '

#  (assuming  both  the  csv  and  the  Rmd  files  are  in  the  same  directory)

tents  <-  read.csv( 'camping-tents.csv ' ,  stringsAsFactors  =  FALSE)


Write R code, using any of the subsetting strategies covered in weeks 3 and 4, to answer the following questions (Note: do not use functions from the package “dplyr”):

a) How many tents have a price less than or equal to $300

b) How many tents have a price between $300 and $400 (including both $300 and $400 prices)?

c) What’s the name of the tent with maximum price. Hint: which.max() is your friend.

d) Select the data of tents with price greater than $400 AND weight less then 1500 grams

e) Calculate the 90th percentile for height and assign it to the object height_p90. Hint: quantile() is your friend.

f) Calculate the 90th percentile for weight and assign it to the object weight_p90. Hint: quantile() is your friend.

g) Select the data of tents with height greater than height_p90 AND weight greater than weight_p90

 

h) Add a new variable weight_lbs to the data frame tents for weight in pounds. Keep in mind that weight is given in grams. Hint: the dollar operator is your friend.

i) Add a new variable height_in to the data frame tents for height in inches. Keep in mind that height is given in centimeters. Hint: the dollar operator is your friend.


 

 

3) Ordered Factor

The  variable  seasons  (in  campping-tents.csv)  is  of data-type  character  with  three levels. You can use functions such as typeof(), unique(), or summary() to inspect this column:


typeof(tents$seasons)

unique(tents$seasons)

table(tents$seasons)


Find out how to use the factor() function in order to convert seasons into an ordinal factor. The resulting levels should be 3-season, 3-4-season, and 4-season (in this order!).

In your answer, in addition to your code—after you create the factor—include the following 3 commands:


summary(tents$seasons)

levels(tents$seasons)

is.ordered(tents$seasons)


 

 

4) Categorizing a quantitative variable

The variable weight (in camping-tents.csv) can be considered to be a quantitative vari- able (handled by R with data-type double).  But what if you want to transform it into a discrete/categorical variable having the nine categories defined by the following intervals:

intervals   labels

(0, 1000]    "1kg"

 


(1000, 2000] (2000, 3000] (3000, 4000] (4000, 5000] (5000, 6000] (6000, 7000] (7000, 8000]


"2kg"

"3kg"

"4kg"

"5kg"

"6kg"

"7kg"

"8kg"


(8000, 9000]    "9kg"

 

The interval (0,  1000] means that the left value 0 is not included, whereas the right value 1000 is included in the interval.

Like in HW1, use cut() to create a factor weight_cut from the values in column weight. And then use weight_cut to plot a barchart.   Include axis labels,  and a title in your graph. Also, display the frenquency (i.e. count) of each category on top of each bar. BTW: weight_cut is not supposed to be an added column to the data table. See this stackoverflow thread for an example of how to display counts in a barplot().

https://stackoverflow.com/questions/59657132/adding-count-in-barplot-in-r

 

 

5) More Technical Questions

Answer the following questions (using your own words). You do NOT need to include any commands, although you may have to run some commands to understand what’s going on.

 

a. What does mean(is.na(x)) tell you about a vector x?

 

b. What does sum(!is.finite(x)) tell you about a vector x?

 

c. Why is x[-which(x  >  0)] not the same as x[x  <=  0]?

 

d. What happens when you subset (i.e. subscript) a vector with a positive integer that’s bigger than the length of the vector?

 

e. What happens when you subset (i.e. subscript) a named vector with a name that does not correspond to any of the elements in the vector you are subsetting?

 

f. A data frame A and a matrix B contain exactly the same tabular data. The first column of A and B is named id. Why B$id fails to return the first column?

 

g. Consider a factor x = factor(c( '1 ' ,  '2 ' ,  '3 ')), and a vector y =  1:3. Why does the comparison x[1] == y[1] returns TRUE?

 

h. Consider the above factor x = factor(c( '1 ' ,  '2 ' ,  '3 ')). Why does the command x +  1 returns missing values: i.e. NA NA NA?