Message-ID: <1392500367.27902.YahooMailNeo@web142605.mail.bf1.yahoo.com>
Date: 2014-02-15T21:39:27Z
From: arun
Subject: trouble using sapply to perform multiple t-test
In-Reply-To: <CAO81HcQG+R_XBY0bK_nw3zGtcRQpX6_-8MfQ60_X4rwzdAJycw@mail.gmail.com>
Hi David,
Try:
Check the output of:
lapply(mm,function(x) x) #mm is matrix
#and
lapply(as.data.frame(mm),function(x) x)
? sapply(split(mm,col(mm)),function(x){out <- t.test(x[1:15],x[16:25])$p.value})
?# ????? 1???????? 2
#0.1091573 1.0000000
#or
?sapply(as.data.frame(mm), function(x)? t.test(x[1:15],x[16:25])$p.value)
#?????? V1??????? V2
#0.1091573 1.0000000
A.K.
On Saturday, February 15, 2014 3:19 PM, David Romano <dromano at stanford.edu> wrote:
Hi folks,
I'm having trouble with code that used to work, and I can't figure out
what's going wrong.? I'd be grateful for any help in sorting this out.
Suppose I define a matrix
> mm <- matrix(1:15, 25,2)
and compare the first 15 values of column 1 of mm to the values remaining
in the same column and obtain p values as follows:
> c1 <- mm[,1]
> out <- t.test(c1[1:15],c1[16:25]) ; out$p.value
This of course works fine, but if I try to embed this line in a call to
sapply to repeat this for each column, I get the following:
> mm.pvals <- sapply(mm, function(x) {out <- t.test(x[1:15],x[16:25]) ;
out$p.value})
Error in t.test.default(x[1:15], x[16:25]) : not enough 'x' observations
What is baffling is code like this has worked for me before, and I can't
tell what's triggering the error.
Thanks in advance for your help!
Best,
David
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
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.