These exercises are about the matrices sections of Introduction to R.
Exercise 1
geneNames <- c("Gene_1", "Gene_2", "Gene_3","Gene_4")
expression <- c(1000, 3000, 10000, 12000)
geneLengths <- c(100, 3000, 200, 1000)
geneMatrix <- cbind(expression,geneLengths)
rownames(geneMatrix) <- geneNames
geneMatrix
## expression geneLengths
## Gene_1 1000 100
## Gene_2 3000 3000
## Gene_3 10000 200
## Gene_4 12000 1000
HINT - We calculated LNE before in vectors exercises’ bonus question
lne <- geneMatrix[,"expression"]/geneMatrix[,"geneLengths"]
geneMatrix <- cbind(geneMatrix,lne)
geneMatrix
## expression geneLengths lne
## Gene_1 1000 100 10
## Gene_2 3000 3000 1
## Gene_3 10000 200 50
## Gene_4 12000 1000 12
## expression geneLengths lne
## Gene_2 3000 3000 1
## Gene_4 12000 1000 12
smallGeneMatrix <- geneMatrix[geneMatrix[,"geneLengths"] > 200 & geneMatrix[,"expression"] > 300,c("expression","lne")]
smallGeneMatrix
## expression lne
## Gene_2 3000 1
## Gene_4 12000 12
GOterms <- c("Positively Regulates Transcription", "Negatively Regulates Transcription", "Positively Regulates Transcription", "Regulates Autophagy")
geneMatrix_GO <- cbind(geneMatrix,GOterms)
geneMatrix_GO
## expression geneLengths lne GOterms
## Gene_1 "1000" "100" "10" "Positively Regulates Transcription"
## Gene_2 "3000" "3000" "1" "Negatively Regulates Transcription"
## Gene_3 "10000" "200" "50" "Positively Regulates Transcription"
## Gene_4 "12000" "1000" "12" "Regulates Autophagy"
## [1] "matrix" "array"
## [1] FALSE
## [1] TRUE
## [1] "matrix" "array"
## [1] TRUE
## [1] FALSE
geneMatrix_transcription <- geneMatrix[grepl("transcription",GOterms, ignore.case = T),]
geneMatrix_transcription
## expression geneLengths lne
## Gene_1 1000 100 10
## Gene_2 3000 3000 1
## Gene_3 10000 200 50
## [1] TRUE
Bonus Question
- Calculate the sum of expression and length columns for only genes with length > 100.
expressionSum <- sum(geneMatrix[geneMatrix[,"geneLengths"] > 100,"expression"])
geneLengthSum <- sum(geneMatrix[geneMatrix[,"geneLengths"] > 100,"geneLengths"])
#OR
expressionAndGeneLengthSum <- colSums(geneMatrix[geneMatrix[,"geneLengths"] > 100,c("expression","geneLengths")])
expressionAndGeneLengthSum
## expression geneLengths
## 25000 4200