Question

In: Computer Science

R Studio Coding Exercise Problem-Set Questions 1-6 # 1) Create the following vector in 1 line...

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.

Solutions

Expert Solution

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)

  


Related Solutions

Use R studio to do this problem. This problem uses the wblake data set in the...
Use R studio to do this problem. This problem uses the wblake data set in the alr4 package. This data set includes samples of small mouth bass collected in West Bearskin Lake, Minnesota, in 1991. Interest is in predicting length with age. Finish this problem without using Im() (a) Compute the regression of length on age, and report the estimates, their standard errors, the value of the coefficient of determination, and the estimate of variance. Write a sentence or two...
Data Structures for R studio Create a numeric vector containing 10 random numbers ranging from 1...
Data Structures for R studio Create a numeric vector containing 10 random numbers ranging from 1 to 10000. Validate that the object you created is a vector Give an example of where you might find this in a biologicall data set. Create a 10-member list containg both numeric and character data. Validate that the object you created is a list Give an example of where you might find this in a biologicall data set. Create a data frame that relates...
1. Basic use of R/R Studio. Solve the following problem in R and print out the...
1. Basic use of R/R Studio. Solve the following problem in R and print out the commands and outputs. (a) Create a vector of the positive odd integers less than 100; Remove the values greater than 60 and less than 80; Find the variance of the remaining set of values (b) What’s the difference in output between the commands 2*1:5 and (2*1):5? Why is there a difference? (c) If you wanted to enter the odd numbers from 1 to 19...
R Programming: create a vector for 1 to 31 and count the number of even and...
R Programming: create a vector for 1 to 31 and count the number of even and odds using ifelse()
( In R / R studio ) im not sure how to share my data set,...
( In R / R studio ) im not sure how to share my data set, but below is the title of my data set and the 12 columns of my data set. Please answer as best you can wheather its pseudo code, partial answers, or just a suggestion on how i can in to answer the question. thanks #---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The dataset incovid_sd_20201001.RDatacontains several variables related to infections of covid-19 for eachzip code in San Diego County as of October...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a rectangular matrix A of dimensions 4X2 (4- rows, 2-columns); Create a matrix transpose to the above matrix A. Call that matrix AT; Calculate matrix products: A*AT and AT*A. Present the results. What are the dimensions of those two product matrices; Square matrixes sometimes have an inverse matrix. Try calculating inverse matrices (or matrixes, if you prefer) of above matrices (matrixes) A*AT and AT*A; Extend...
please use R studio to answer the following questions 1. An eductional theorist collects behavioural data...
please use R studio to answer the following questions 1. An eductional theorist collects behavioural data from two groups of children in an early childhood center. She measures how much time the children are active (e.g. running or swinging on the monkey bars) in minutes. The first group of children are encouraged to run about and as such are expected to be active; the second group is encouraged to sit still and paint, and are expected to be less active....
** Number 2 implemented in R (R Studio) ** Set up the Auto data: Load the...
** Number 2 implemented in R (R Studio) ** Set up the Auto data: Load the ISLR package and the Auto data Determine the median value for mpg Use the median to create a new column in the data set named mpglevel, which is 1 if mpg>median and otherwise is 0. Make sure this variable is a factor. We will use mpglevel as the target (response) variable for the algorithms. Use the names() function to verify that your new column...
Use R studio to answer the following questions location 1 location 2 63.9 66.8 71.3 63.8...
Use R studio to answer the following questions location 1 location 2 63.9 66.8 71.3 63.8 70.6 60.7 54.6 66.2 63.7 59.5 61 59.5 69.8 66.1 72.4 64.6 64.4 64.8 69.8 61.6 65.6 61.3 62.8 64.9 63.6 66.2 Temperatures are extremely variable in sections of the Colorado River near the Glen Canyon Dam.  Researchers measure temperature in two locations (1 and 2) with samples of size n1 = 10 and n2 = 16, and calculate variances of s12 = 20 °C...
Using R studio 1. Read the iris data set into a data frame. 2. Print the...
Using R studio 1. Read the iris data set into a data frame. 2. Print the first few lines of the iris dataset. 3. Output all the entries with Sepal Length > 5. 4. Plot a box plot of Petal Length with a color of your choice. 5. Plot a histogram of Sepal Width. 6. Plot a scatter plot showing the relationship between Petal Length and Petal Width. 7. Find the mean of Sepal Length by species. Hint: You could...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT