In: Computer Science
R Studio Coding Exercise Problem-Set Questions 1-6
# 1) Create the following vector in 1 line of code without using the c() function:
# [i] 4 12 20 4 12 20 4 12
# 2) Create a vector of 25 random heights between 54 and 78 inches. Cycle through the vector using a For loop and create a new vector that places each height into a category. People less than 5 feet should be categorized as short, those taller than 6 feet should be categorized as tall, and everyone else should be categorized as average.
# Load the dataset called diamonds which is housed in the
ggplot2 package. To do so execute the following steps.
If you have not done so previously, install ggplot 2:
install.packages(ggplot)
Load the package: library(ggplot2)
Load the data: data("diamonds")
Diamonds is stored as something called a tibble. Coerce diamonds
into a data frame using this code:
diamonds<-as.data.frame(diamonds)
# 3) Write code to create a new data frame that is composed of just the diamonds that are Ideal cut.
# 4) Write code to calculate the number of diamonds in the data set
# 5) Write code to calculate the median price of Premium cut, E
color diamonds
# 6) Write code to create a histogram showing the distribution of
prices of diamonds that are greater than 2 carats.
Refer the screenshots for better understanding
Ans 1: rep(seq(4,20,by = 8), times = 3, length.out = 8)
Ans 2: for random number generation:
> x <- runif(25, min = 54, max = 78)
Vector "new" consists the required sections:
> new <- c()
> for (i in x) {
+ if(i < 60) {
+ new <- c(new,'S')
+ }
+ else if (i > 72) {
+ new <- c(new,'T')
+ }
+ else {
+ new <- c(new,'A')
+ }
}
Ans 3: Dataframe "newdataframe" consists the required sections of diamonds.
> newdf <- diamonds$cut == 'Ideal'
> newdataframe <- diamonds[newdf,]
> newdataframe
Ans 4: > nrow(diamonds)
Ans 5: premium and ecolor are boolean vectors while Premium and EandPre are numeric vectors.
> premium <- diamonds$cut == "Premium"
> Premium <- diamonds[premium,]
> ecolor <- Premium$color == "E"
> EandPre <- Premium[ecolor,]
> median(EandPre$price)
Ans 6: carat is a boolean vector and Carat is a dataframe.
> carat <- diamonds$carat > 2
> Carat <- diamonds[carat,]
> y <- Carat$price
> hist(y)