''list' object cannot be coerced to type 'logical' error using R
I want to write a loop that loops through all values of the variable u (unique transcript cluster ID). In each loop iteration, I want to:
(1) Get the correct mapping between the ith transcript cluster ID and the exon probe IDs
(2) Subset the gene and exon matrices by the appropriate transcript cluster ID or exon probe IDs.
(3) Call the exon.ni function
(4) Store the output p-values, test statistics, and CIs for each cluster ID
For each loop, I also want to add an if statement to conduct this calculation only if I have at least 2 rows in the d.exon matrix.
My code
> # Get unique transcript cluster IDs (gene IDs) from annotation file
> u <- unique(as.character(map$transcript_cluster_id))
> u <- intersect(u, dimnames(g)[[1]])
This is the exon ni value calculation and t-test function. It takes transcript cluster values, exon probes for this transcript cluster, and classification vector.
> exon.ni <- function(genex, exonx, rx) {
+ ni <- t(t(exonx) -genex)
+ ttest <- t(apply(ni, 1, t.two, sam=as.logical(r), v=F))
+ return(ttest)
+ }
This is the two-sample t-test function
> t.two <- function(x, sam, v=F) {
+ x <- as.numeric(x)
+ out <- t.test(as.numeric(x[sam]), as.numeric(x[!sam]), alternative="two.sided", var.equal=v)
+ o <- as.numeric(c(out$statistic, out$p.value, out$conf.int[1], out$conf.int[2]))
+ names(o) <- c("test_statistic", "pv", "lower_ci", "upper_ci")
+ return(o)
+ }
Then, I want to calculate the ni p-values
> ni.out <- for (i in u) {
+ ex <- dimnames(map[map$transcript_cluster_id %in% i,])[[1]]
+ d.exon <- as.numeric(unlist(e[ex,]))
+ d.gene <- as.numeric(unlist(g[i,]))
+ if(!is.null(1:colnames(d.exon)) & 1:colnames(d.exon) >= 2) {
+ exon.ni(genex=as.numeric(d.gene), exonx=d.exon, rx=r)
+ }
+ }
Traceback:
Error in t.test(as.numeric(x[sam]), as.numeric(x[!sam]), alternative = "two.sided", :
'list' object cannot be coerced to type 'logical'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
