############################################################################### # Answers to exercises in Chapter 1: Brief Introduction to R ############################################################################### # install.packages("BurStMisc") library("BurStMisc") gene1 <- c(1.00,1.50,1.25) gene2 <- c(1.35,1.55,1.00) gene3 <- c(-1.10,-1.50,-1.25) gene4 <- c(-1.20,-1.30,-1.00) rowColNames <- list(c("gene1","gene2","gene3","gene4"), c("Eric","Peter","Anna")) geneData <- matrix(c(gene1,gene2,gene3,gene4), nrow=4, ncol=3, byrow=TRUE, dimnames = rowColNames) # 2. geneData # (a) apply(geneData,2,sd) # (b) apply(geneData,1,sd) # (c) To order the data frame according to the gene standard deviations. sdexprsval <- apply(geneData,1,sd) o <- order(sdexprsval,decreasing=TRUE) corner(geneData[o,]) # (d) geneData[o[1],] gene2 # 3. Computations on gene means of the Golub data. # (a) Computation of mean gene expression values. data(golub, package = "multtest") golubRowMeans <- apply(golub,1,mean) # (b) To order the data frame use o <- order(golubRowMeans,decreasing=TRUE) corner(golub[o,]) # (c) Give the names of the three genes with the largest mean expression value. golub.gnames[o[1:3],3] # (d) Give their biological names. golub.gnames[o[1:3],2] # 4. Computations on gene standard deviations of the Golub data. # (a) The standard deviation per gene can be computed by sdPerGene <- apply(golub,1,sd) # (b) The gene with standard deviation larger than 0.5 can be selected by sdPerGeneGreaterThan0.5 <- golub[sdPerGene>0.5,] # (c) sum(sdPerGene>0.5) gives that the number of genes having sd larger than 0.5 is 1498. sum(sdPerGene>0.5) # 5. Oncogenes in Golub data. # (a) length(grep("oncogene",golub.gnames[,2])) gives 47. length(grep("oncogene",golub.gnames[,2]), ignore.case = TRUE) # (b) By the script below the "Cellular oncogene c-fos is found. data(golub, package="multtest") rowIndex <- grep("oncogene",golub.gnames[,2], ignore.case = TRUE) golubOncogenes <- golub[rowIndex,] golubOncogenes.gnames <- golub.gnames[rowIndex,] golubFactor <- factor(golub.cl,levels=0:1, labels= c("ALL","AML")) golubRowMeans <- apply(golubOncogenes[,golubFactor=="ALL"],1,mean) o <- order(golubRowMeans,decreasing=TRUE) golubOncogenes.gnames[o[1:3],2] # (c) golubRowMeans <- apply(golubOncogenes[,golubFactor=="AML"],1,mean) o <- order(golubRowMeans,decreasing=TRUE) golubOncogenes.gnames[o[1:3],2] # (d) Writing results to a csv file. Be aware of the correct column separation. x <- golubOncogenes.gnames[o[1:10],c(3,2)] colnames(x) <- c("probe ID","gene name") write.csv(x,file="~/biology664/golubOncogenes.csv") write.table(x,file="~/biology664/golubOncogenes.table", row.names=FALSE) # 6. Constructing a factor. # (a) gl(2,4) # (b) gl(5,3) # (c) gl(3,5) # 7. Gene means for B1 patients. library(ALL); data(ALL) meanB1 <- apply(exprs(ALL[,ALL$BT=="B1"]),1, mean) o <- order(meanB1,decreasing=TRUE) meanB1[o[1:3]]