Question

In R-studio , Use a for loop to iterate over all rows of the mtcars dataset...

In R-studio , Use a for loop to iterate over all rows of the mtcars dataset to create a car objectfor each row. Use the name of the row, and the values for mpg, cyl, and hp. You canignore the columns not needed for the construction of our car object. After creatingthe car object use the print() function to print it.

The mtcars data set is already integrated in R.

That is what I am using for this question

Homework Answers

Answer #1

#Import the pre loaded datasets
data()

#Initialize vectord
cnames <- vector()
carmpg <- vector()
carcyl <- vector()
carhp <- vector()

#Loop mtcars for required car object attributes "mpg","cyl","hp"
for(i in 1:nrow(mtcars))
{
carmpg <- append(carmpg, mtcars[i,'mpg'])
carcyl <- append(carcyl, mtcars[i,'cyl'])
carhp <- append(carhp, mtcars[i,'hp'])
}

#Loop row names in mtcars for car object attributes "names"
cnames <- rownames(mtcars)

#Create thr carobject
carobjects <- list(objcarnames=cnames,objcarcyl=carcyl,objcarmpg=carmpg,objcarhp=carhp)

#Print the car object detaiils
for(object in carobjects)
{
print(paste("Car Names,cyl,mpg,hp is",carobjects$objcarnames,carobjects$objcarcyl,carobjects$objcarmpg,carobjects$objcarhp))
  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This problem requires the use of R-Studio. Consider the mtcars data. In R, you can use...
This problem requires the use of R-Studio. Consider the mtcars data. In R, you can use the following code to get the data: dta <- mtcars Use ?mtcars to read the information about this data set. In what follows, we will fit the regression model: mpgi=β0 + β0vsi+ εi, i=1,2,...,n. Note that, vs is categorical variable, whose value is 1 if the observed car has the V-shaped engine or 0 otherwise. (A) Which of the following is the most accurate...
(1) Write R code to install the package named "MASS" only if the package is not...
(1) Write R code to install the package named "MASS" only if the package is not already installed on the machine. Then attach that package to your current workspace to use it in your programming. Your code below (2) Write R code to find what datasets are available only for the MASS package by creating a file named "R data sets". Your code below (3) Write R code to bring up the documentation page for the "Boston" dataset. Your code...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...