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

R-Studio (R Programming Language) 1. How would you create a vector `V` containing the values 0,...
R-Studio (R Programming Language) 1. How would you create a vector `V` containing the values 0, 0.25, 0.5, 0.75, and 1?    ```{r} #insert your code ``` 2. Name the elements of `V`: first, second, middle, fourth, last. Describe two ways of naming elements in `V` ```{r} #insert your code ``` 3. Suppose you keep track of your mileage each time you fill up. At your last 6 fill-ups the mileage was 65311 65624 65908 66219 66499 66821 67145 67447....
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...
Use R code to do the following!!!! 1. Create a vector v1 that will contain integers...
Use R code to do the following!!!! 1. Create a vector v1 that will contain integers from -30 to 60. 2. Copy v1 into a vector v2 and add names 'odd' or 'even' based on the value. 3. Copy v1 into a vector v3 and if the number can be divided by 3, replace it by 'NA'. 4. Assign the mean of v3 to v4 ignoring the 'NA'.
use R # Problem 4 (5 pts each): # Set x as a vector of 500...
use R # Problem 4 (5 pts each): # Set x as a vector of 500 random numbers from Unif(100,300). # This vector will be kept fixed for the rest of this problem. # # (a) Define a function b1(x, beta0, beta1, sigm) that uses the lm() function to # return the regression line slope b1 for y as a linear function of x, where # # y = beta0 + beta1 x + err # # and the error...
( 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...
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()
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT