'R: Error in `[.default`(x, s1) : invalid subscript type 'list'

I want to perform ANOVA test for each column variable - ctl, schiz, bp, dep - which can be found in the Profile column in the ann dataframe. The first eigenvector d$C1 is the continuous variable and the different levels of the column variable as the grouping variable.

d <- read.table("filtered.mds", header=T)
ann <- read.table("clinical_table.txt", header=T, sep="\t")


# Create new dataframe
dat <- t(cbind(d$C1,d$C2))
colnames(dat) <- paste(ann$Profile, data.table::rowid(ann$Profile), sep="_")
rownames(dat) <- c("C1", "C2")

# Levels
ctl <- grepl("^Unaffected control", ann$Profile)
schiz <- grepl("^Schiz.", ann$Profile)
bp <- grepl("^BP", ann$Profile)
dep <- grepl("^Dep.", ann$Profile)


# 1-factor ANOVA with 4 levels
aov.lvl <- function(x, s1, s2, s3, s4){
  x1 <- as.numeric(x[s1])
  x2 <- as.numeric(x[s2])
  x3 <- as.numeric(x[s3])
  x4 <- as.numeric(x[s4])
  fac <- c(rep("A", length(x1)), rep("B", length(x2)), rep("C", length(x3)), rep("D", length(x4)))
  a.dat <- data.frame(as.factor(fac), c(x1,x2,x3,x4))
  names(a.dat) <- c("factor", "express")
  p.out <- summary(data.frame(aov(express~factor)))[[1]][1,5]
  return(p.out)
}

aov.run <- apply(dat, 1, aov.lvl, s1=ctl, s2=schiz, s3=bp, s4=dep)

Traceback:

Error in `[.default`(x, s1) : invalid subscript type 'list'
Called from: NextMethod("[")
Browse[1]> Q
> traceback()
8: NextMethod("[")
7: `[.formula`(x, s1) at #2
6: x[s1] at #2
5: aov(express ~ factor) at #9
4: data.frame(aov(express ~ factor)) at #9
3: summary(data.frame(aov(express ~ factor))) at #9
2: FUN(newX[, i], ...)
1: apply(dat, 1, aov.lvl, s1 = ctl, s2 = schiz, s3 = bp, s4 = dep)

D (dataframe)

> dput(d[1:20,1:5])
structure(list(FID = c("AC10", "AC11", "AC12", "AC13", "AC14",
"AC15", "AC17", "AC18", "AC19", "AC1", "AC20", "AC21", "AC22",
"AC23", "AC24", "AC25", "AC26", "AC27", "AC29", "AC2"), IID = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), SOL = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), C1 = c(-0.00385609,
0.0101138, -0.0146168, -0.0218236, -0.0134745, -0.017089, 0.0152448,
0.0134359, 0.00540102, -0.0125389, 0.00463956, -0.00416079, -0.000325898,
0.0132781, 0.0130666, 0.00718399, -0.0051912, -0.0227934, 0.0364974,
-0.0180301), C2 = c(0.000865365, -0.001752, 0.0189917, -0.023343,
-0.0340531, -0.0258976, -0.00794043, 0.0173163, 0.00639341, -0.0343077,
0.01083, -0.0402179, 0.0158751, -0.00262893, -0.0216757, -0.00261259,
-0.00542089, -0.00515714, 0.0105216, -0.0193606)), row.names = c(NA,
20L), class = "data.frame")

Ann (dataframe)

> dput(ann$Profile)
c("Schiz.", "Schiz.", "Schiz.", "BP", "BP", "Unaffected control",
"Schiz.", "BP", "Unaffected control", "BP", "BP", "BP", "Schiz.",
"BP", "Unaffected control", "Schiz.", "Schiz.", "Unaffected control",
"Unaffected control", "BP", "Unaffected control", "Schiz.", "BP",
"Unaffected control", "BP", "Unaffected control", "BP", "Schiz.",
"Unaffected control", "Schiz.", "Schiz.", "Schiz.", "Schiz.",
"BP", "Unaffected control", "Schiz.", "BP", "Schiz.", "BP", "Unaffected control",
"BP", "Unaffected control", "Unaffected control", "Unaffected control",
"Unaffected control", "Schiz.", "Unaffected control", "BP", "BP",
"BP", "Unaffected control", "BP", "BP", "BP", "BP", "Unaffected control",
"Schiz.", "Unaffected control", "BP", "BP", "Unaffected control",
"Unaffected control", "BP", "Schiz.", "BP", "Schiz.", "BP", "Unaffected control",
"Schiz.", "Unaffected control", "Schiz.", "Unaffected control",
"Schiz.", "Schiz.", "Unaffected control", "Unaffected control",
"Unaffected control", "Schiz.", "Schiz.", "BP", "BP", "Unaffected control",
"Unaffected control", "Schiz.", "Schiz.", "Schiz.", "Schiz.",
"BP", "Unaffected control", "BP", "Unaffected control", "BP",
"Schiz.", "Schiz.", "Schiz.", "Unaffected control", "Unaffected control",
"Schiz.", "Unaffected control", "Unaffected control", "Unaffected control",
"BP", "BP", "Dep.", "Unaffected control", "Unaffected control",
"Dep.", "Dep.", "Dep.", "Dep.", "Dep.", "Unaffected control",
"Unaffected control", "Schiz.", "Dep.", "BP", "Dep.", "Schiz.",
"Schiz.", "Schiz.", "BP", "Unaffected control", "Unaffected control",
"Unaffected control", "BP", "BP", "Dep.", "Schiz.", "Dep.", "BP",
"Unaffected control", "Unaffected control", "Schiz.", "Schiz.",
"Unaffected control", "Unaffected control", "BP", "BP", "Schiz.",
"Dep.", "BP", "Dep.", "BP", "Schiz.", "Unaffected control", "Dep.",
"BP", "Schiz.", "Dep.", "Dep.", "BP", "BP", "Schiz.")
r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source