tag:blogger.com,1999:blog-8486716216590360743.post7037539006531122599..comments2023-03-25T07:53:32.369-07:00Comments on R, Ruby, Perl und ich: R - t.test for subsets of a data frame (ddply or tapply)mvogel78http://www.blogger.com/profile/15369579964873138094noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-8486716216590360743.post-68608127852432147972016-11-08T05:58:27.256-08:002016-11-08T05:58:27.256-08:00yes, i was looking for the same thing. do you have...yes, i was looking for the same thing. do you have an idea how to implement this? i have been trying to add an addition variable like this<br />t.test.plyr <- function(x, var,group){<br /> y <- rep(NA,10)<br /> y[6] <- nrow(x)[1] # count observations<br /> if(nrow(x) < 2) return(y) # exits if too less observations<br /> res <- t.test(x[var]~x[group]) # doing the test<br /><br /> y[1] <- res$statistic # extract values of interest<br /> y[2] <- res$p.value <br /> y[3] <- res$estimate <br /> y[4] <- res$conf.int[1] <br /> y[5] <- res$conf.int[2] <br /> y[7] <- res$parameter <br /> y[8] <- res$method <br /> y[9] <- res$alternative <br /> y[10] <- res$null.value <br /><br /> names(y) <- c("statistic","p.value","estimate","conf.int1", "conf.int2", "nobs","dof","method","alternative","null.value")<br /> y <br />}<br /><br />result <- ddply(df, .(group), t.test.plyr, "sds", "sex")<br />but its giving me <br />Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") : <br /> missing value where TRUE/FALSE needed<br />In addition: Warning messages:<br />1: In mean.default(x) : argument is not numeric or logical: returning NA<br />2: In var(x) :<br /> Calling var(x) on a factor x is deprecated and will become an error.<br /> Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.<br />3: In mean.default(y) : argument is not numeric or logical: returning NA<br />4: In var(y) :<br /> Calling var(x) on a factor x is deprecated and will become an error.<br /> Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.<br />Called from: t.test.default(x = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, <br />1L, 1L, 1L), y = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, <br />1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L))<br /><br />Any Ideas?Mariehttps://www.blogger.com/profile/11368577764946455247noreply@blogger.comtag:blogger.com,1999:blog-8486716216590360743.post-7008017664413283032013-10-10T05:34:11.587-07:002013-10-10T05:34:11.587-07:00do you mean something like this?
> xx <- da...do you mean something like this?<br /><br />> xx <- data.frame(vals=rnorm(100),group=sample(letters[1:2],100,replace = T))<br /><br />> t.test(xx$vals~xx$group)<br /><br /> Welch Two Sample t-test<br /><br />data: xx$vals by xx$group<br />t = 0,6923, df = 84,572, p-value = 0,4906<br />alternative hypothesis: true difference in means is not equal to 0<br />95 percent confidence interval:<br /> -0,2801770 0,5794806<br />sample estimates:<br />mean in group a mean in group b <br /> 0,02413266 -0,12551915 mvogel78https://www.blogger.com/profile/15369579964873138094noreply@blogger.comtag:blogger.com,1999:blog-8486716216590360743.post-90837014409643460662013-10-05T07:51:49.456-07:002013-10-05T07:51:49.456-07:00What you are doing here is basically testing if an...What you are doing here is basically testing if any value has a mean significantly different from zero, right? Would it not be more interesting to test the groups against each other? <br /><br />Actually, that's what I wanted to code when your blog showed up as a search result.Anonymousnoreply@blogger.com