I need help on 4b please :( 4.) ?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. a.) Write a code (Hint: a logical expression) that extracts the observations with wool type A and tension level M. Assign it to the object AM.warpbreaks *(This is done)* 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! :) -- View this message in context: http://r.789695.n4.nabble.com/how-to-find-the-mean-and-sd-tp4712141.html Sent from the R help mailing list archive at Nabble.com.
On Fri, 2015-09-11 at 07:48 -0700, massmatics wrote:> AM.warpbreaks<=30The above command is not returning what you expected, what part of the AM.warpbreaks dataframe is expected to be <= 30? Effectively you are using a two stage process. 1) Create a logical vector identifying rows in the dataframe with a breaks value <= 30 2) use the vector in 1. to extract just the rows you are interested in and use that to calculate the mean of the breaks column.
?tapply or better yet ?ave ## a wrapper for tapply allows it to be done without extraction. Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Fri, Sep 11, 2015 at 9:02 AM, Tom Wright <tom at maladmin.com> wrote:> On Fri, 2015-09-11 at 07:48 -0700, massmatics wrote: >> AM.warpbreaks<=30 > > The above command is not returning what you expected, what part of the > AM.warpbreaks dataframe is expected to be <= 30? > > Effectively you are using a two stage process. > 1) Create a logical vector identifying rows in the dataframe with a > breaks value <= 30 > 2) use the vector in 1. to extract just the rows you are interested in > and use that to calculate the mean of the breaks column. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
massmatics, You are trying to take the mean/sd of an entire data.frame and therefore you receive an error. You must do some form of subset and take the mean of the 'breaks' column. This can be done a few ways (as with almost anything in R). AM.warpbreaks2 <- subset(AM.warpbreaks, breaks <= 30) mean(AM.warpbreaks2$breaks) or mean(AM.warpbreaks$breaks[Am.warpbreaks$breaks <= 30]) or more concisely with(AM.warpbreaks, mean(breaks[breaks <= 30])) Again, the main point here is that you need to specify the column when working with a data.frame object. Regards, Charles On Fri, Sep 11, 2015 at 11:02 AM, Tom Wright <tom at maladmin.com> wrote:> On Fri, 2015-09-11 at 07:48 -0700, massmatics wrote: > > AM.warpbreaks<=30 > > The above command is not returning what you expected, what part of the > AM.warpbreaks dataframe is expected to be <= 30? > > Effectively you are using a two stage process. > 1) Create a logical vector identifying rows in the dataframe with a > breaks value <= 30 > 2) use the vector in 1. to extract just the rows you are interested in > and use that to calculate the mean of the breaks column. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]