massmatics
massmatics

Reputation: 21

How to compute for the mean and sd

I need help on 4b please

  1. ‘Warpbreaks’ is a built-in dataset in R. Load it using the function data(warpbreaks). It consists of the number of warp breaks per loom, where a loom corresponds to a fixed length of yarn. It has three variables namely, breaks, wool, and tension.

    b. For the ‘AM.warpbreaks’ dataset, compute for the mean and the standard deviation of the breaks variable for those observations with breaks value not exceeding 30.

    data(warpbreaks)
    warpbreaks <- data.frame(warpbreaks)
    AM.warpbreaks <- subset(warpbreaks, wool=="A" & tension=="M")
    
    mean(AM.warpbreaks<=30)
    sd(AM.warpbreaks<=30)
    

This is what I understood this problem and typed the code as in the last two lines. However, I wasn't able to run the last two lines while the first 3 lines ran successfully. Can anybody tell me what is the error here? Thanks! :)

Upvotes: 1

Views: 351

Answers (2)

Badger
Badger

Reputation: 1053

Another way to go about it: This way you aren't generating a bunch of datasets and then working on remembering which is which. This is more a personal thing though.

data(warpbreaks)
mean(AM.warpbreaks[which(AM.warpbreaks$breaks<=30),"breaks"])
sd(AM.warpbreaks[which(AM.warpbreaks$breaks<=30),"breaks"])

Upvotes: 1

user295691
user295691

Reputation: 7248

There are two problems with your code. The first is that you are comparing to 30, but you're looking at the entire data frame, rather than just the "breaks" column.

AM.warpbreaks$breaks <= 30

is an expression that refers to the breaks being less than thirty.

But mean(AM.warpbreaks$breaks <= 30) will not give the answer you want either, because R will evaluate the inner expression as a vector of boolean TRUE/FALSE values indicating whether that break is less than 30.

Generally, you just want to take another subset for an analysis like this.

AM.lt.30 <- subset(AM.warpbreaks, breaks <= 30)
mean(AM.lt.30$breaks)
sd(AM.lt.30$breaks)

Upvotes: 0

Related Questions