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...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT