[R] apply a function to loop through
Weiwei Shi
helprhelp at gmail.com
Tue Jul 17 21:57:09 CEST 2007
Hi,
first of all, your Scores are factors instead of numeric, you need to change it;
I made a new myDat for test purpose by
myDat = as.data.frame(rbind(myDat, myDat))
myDat[16:30, 3] <- "gene2"
> myDat
tissue Scores Grp
a A 3.01494535196933 gene1
b A 2.99647624484379 gene1
c A 3.00527533284709 gene1
d A 3.02321059636917 gene1
e A 3.04694197334289 gene1
f B 11.1464974692841 gene1
g B 12.2372838904939 gene1
h B 11.7277801841221 gene1
i B 11.6405683605147 gene1
j B 11.1631961720026 gene1
k C 0.67528704974662 gene1
l C 1.21000950157251 gene1
m C 0.843722400594721 gene1
n C 0.881706314004343 gene1
o C 1.43670211710054 gene1
a1 A 3.01494535196933 gene2
b1 A 2.99647624484379 gene2
c1 A 3.00527533284709 gene2
d1 A 3.02321059636917 gene2
e1 A 3.04694197334289 gene2
f1 B 11.1464974692841 gene2
g1 B 12.2372838904939 gene2
h1 B 11.7277801841221 gene2
i1 B 11.6405683605147 gene2
j1 B 11.1631961720026 gene2
k1 C 0.67528704974662 gene2
l1 C 1.21000950157251 gene2
m1 C 0.843722400594721 gene2
n1 C 0.881706314004343 gene2
o1 C 1.43670211710054 gene2
> mytry <- by(myDat, INDICES=as.factor(myDat[,3]), FUN=function(x) {
+ pairwise.wilcox.test(as.numeric(as.character(x$Scores)),
+
+ x$tissue,
+
+ p.adjust.method = "none")$p.value
+ })
> mytry
as.factor(myDat[, 3]): gene1
A B
B 0.007936508 NA
C 0.007936508 0.007936508
------------------------------------------------------------
as.factor(myDat[, 3]): gene2
A B
B 0.007936508 NA
C 0.007936508 0.007936508
HTH,
Weiwei
On 7/17/07, Hai Lin <kevinvol2002 at yahoo.com> wrote:
> Dear R users,
>
> I have a dataset generated as follows,
>
> myDat <- data.frame(matrix(c(rep(LETTERS[1:3],
> each=5),
> rnorm(5,mean=3,sd=0.03),
> rnorm(5,12,1),
> rnorm(5,1,0.5)),
> ncol=2,
>
> dimnames=list(c(letters[1:15]),
>
> c("tissue","Scores"))))
> myDat$Grp <-c("gene1")
>
> There is one level "gene1" in $Grp in my data step.
> I'd like to do pairwise.wilcox.test on $tissue while
> going throug $Grp if there are more levels with gene2,
> gene3.
>
> I tried to loop through $Grp using apply with an error
> message "Error in sort(unique.default(x), na.last =
> TRUE) :
> 'x' must be atomic".
>
> mytry <- apply(as.matrix(myDat),
> 1,
>
> function(Grp)pairwise.wilcox.test(Grp$Scores,
>
> Grp$tissue,
>
> p.adjust.method = "none")$p.value)
>
> I could not find any similar stuffs in forum. Could
> anyone here give a hand?
>
> Thanks a bunch.
>
> kevin
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
--
Weiwei Shi, Ph.D
Research Scientist
GeneGO, Inc.
"Did you always know?"
"No, I did not. But I believed..."
---Matrix III
More information about the R-help
mailing list