From b411b2a0fcde3c04897e3a07af4b64357b922d72 Mon Sep 17 00:00:00 2001
From: root Introduction
to R.
These exercises cover the matrices sections of Introduction to R.
Exercise 1
These exercises cover the matrices sections of Introduction to R.
Exercise 1
## [1] "2024-12-12 02:00:20 UTC"
+## [1] "2024-12-12 18:12:15 UTC"
We can also use the arithmetic operations with our time objects.
-## [1] "2024-12-12 02:00:20 UTC"
+## [1] "2024-12-12 18:12:15 UTC"
-## [1] "2024-12-12 01:58:20 UTC"
+## [1] "2024-12-12 18:10:15 UTC"
-## Time difference of 0.1078458 secs
+## Time difference of 0.1084907 secs
We can also change the timezone by specifying a tz parameter
-## [1] "02 O'Clock AM Thursday on December 12th"
+## [1] "18 O'Clock PM Thursday on December 12th"
-## [1] "02 O'Clock AM Thursday on December 12th"
+## [1] "18 O'Clock PM Thursday on December 12th"
Most of the time we can convert more complex object back to our basic object types we are more familar with.
-## [1] "2024-12-12 02:00:20.799085"
+## [1] "2024-12-12 18:12:15.725485"
-## [1] 0.1078458
+## [1] 0.1084907
## Mean is -0.0473813821205204
-## [1] 2.066187
+## Mean is 0.260665894154018
+## [1] -0.2426098
These custom functions can also be utilized with apply.
-## [1] 2.06618698 0.17165772 -0.71309305 0.04463075 -1.24264432 0.21368949
-## [7] -0.82084457 0.89006732 -0.57284618 -0.46639582 -1.28333645 0.98048258
-## [13] 1.31683429 -0.36486729 -0.02032230 -0.68673550 0.13403087 1.02971875
-## [19] 1.13413397 -1.81034725
+## [1] -0.24260979 0.37093999 0.58712023 0.81503665 0.92783453 0.54524311
+## [7] -1.15824154 -0.38544962 0.08283829 0.73612828 -1.39531982 -0.48690779
+## [13] -2.08700758 1.41872341 -0.02320079 0.08587476 1.50147491 -1.67344992
+## [19] 0.86495743 -0.48398475
+ +
+These exercises are about the conditions and loops sections of Introduction +to R.
+Exercise 1 - If Else
+x<- 42
+
+if (x<0){
+ print("It's a negative number!")
+}
+
+x <- -42
+
+if (x<0){
+ print("It's a negative number!")
+}
## [1] "It's a negative number!"
+x <- 0
+
+if (x<0){
+ print("It's a negative number!")
+}else{
+ print("It's not a negative number!")
+}
## [1] "It's not a negative number!"
+x <- -1
+
+if (x<0){
+ print("It's a negative number!")
+}else{
+ print("It's not a negative number!")
+}
## [1] "It's a negative number!"
+x <- -1
+
+if (x<0){
+ print("It's a negative number!")
+}else if (x==0){
+ print("It's zero")
+}else{
+ print("It's a positive number!")
+}
## [1] "It's a negative number!"
+x <- 0
+
+if (x<0){
+ print("It's a negative number!")
+}else if (x==0){
+ print("It's zero")
+}else{
+ print("It's a positive number!")
+}
## [1] "It's zero"
+x <- 1
+
+if (x<0){
+ print("It's a negative number!")
+}else if (x==0){
+ print("It's zero")
+}else{
+ print("It's a positive number!")
+}
## [1] "It's a positive number!"
+Hint: The modulus operator may be useful here i.e. x%%2 returns +the remainder after the value of x is divided by 2.
+ +## [1] "1 is odd"
+
+## [1] "2 is even"
+– Using an ifelse() expression, create a factor from a vector of 1 to +40 where all numbers less than 10 are “small”,10 to 30 are “mid”,31 to +40 are “big”
+ +## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
+## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
+vectorResult <- ifelse(condExercise<10,"small",ifelse(condExercise < 31,"mid","big"))
+temp <- factor(vectorResult,levels=c("small","mid","big"),order=T)
+temp
## [1] small small small small small small small small small mid mid mid
+## [13] mid mid mid mid mid mid mid mid mid mid mid mid
+## [25] mid mid mid mid mid mid big big big big big big
+## [37] big big big big
+## Levels: small < mid < big
+– Calculate the factorial (factorial of 3 = 3 * 2 * 1) of 10 using a +loop.
+for(x in 1:10){
+ if(x == 1){
+ factorialAnswer <- 1
+ }else{
+ factorialAnswer <- factorialAnswer * x
+ }
+}
+factorialAnswer
## [1] 3628800
+– Adjusting your answer from before, what is the first number that +has a factorial greater than 1000.
+factorialAnswer <- 0
+count <- 0
+
+while(factorialAnswer <= 1000){
+ count <- count+1
+ if(count == 1){
+ factorialAnswer <- 1
+ }else{
+ factorialAnswer <- factorialAnswer * count
+ }
+}
+count
## [1] 7
+filesToRead <- dir("ExpressionResults/",pattern = "*\\.txt",full.names=T)
+fileRead <- vector("list",length=length(filesToRead))
+
+for(i in 1:length(filesToRead)){
+ fileRead[[i]] <- read.delim(filesToRead[i],header=F,sep="\t")
+ colnames(fileRead[[i]]) <- c("GeneNames",basename(filesToRead[i]))
+}
+mergedTable <- NULL
+for(i in fileRead){
+ if(is.null(mergedTable)){
+ mergedTable <- i
+ }else{
+ mergedTable <- merge(mergedTable,i,by=1,all=T)
+ }
+
+ print(nrow(mergedTable))
+}
## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+
+## GeneNames Annotation.txt NA ExpressionResults_Sample1.txt
+## 1 GeneName Ensembl Pathway NA
+## 2 Gene_1 Ens_1001 DNA_Binding 3.448466
+## 3 Gene_10 Ens_10010 DNA_Binding 5.314180
+## ExpressionResults_Sample10.txt ExpressionResults_Sample2.txt
+## 1 NA NA
+## 2 7.665488 5.250063
+## 3 7.813501 5.361170
+## ExpressionResults_Sample3.txt ExpressionResults_Sample4.txt
+## 1 NA NA
+## 2 5.968927 6.868251
+## 3 5.305980 6.742855
+## ExpressionResults_Sample5.txt ExpressionResults_Sample6.txt
+## 1 NA NA
+## 2 5.367100 5.189686
+## 3 5.957786 6.293098
+## ExpressionResults_Sample7.txt ExpressionResults_Sample8.txt
+## 1 NA NA
+## 2 3.882930 5.329258
+## 3 7.361497 6.649428
+## ExpressionResults_Sample9.txt
+## 1 NA
+## 2 6.167451
+## 3 6.213910
+– Add annotation from Annotation.txt. How do the pathway information +for genes compare between expression table and annotation table.
+Annotation <- read.table("ExpressionResults/Annotation.txt",sep="\t",h=T)
+annotatedExpression <- merge(Annotation,mergedTable,by=1,all.x=F,all.y=T)
+annotatedExpression[1:2,]
## GeneName Ensembl Pathway Annotation.txt NA
+## 1 GeneName <NA> <NA> Ensembl Pathway
+## 2 Gene_1 Ens_1001 DNA_Binding Ens_1001 DNA_Binding
+## ExpressionResults_Sample1.txt ExpressionResults_Sample10.txt
+## 1 NA NA
+## 2 3.448466 7.665488
+## ExpressionResults_Sample2.txt ExpressionResults_Sample3.txt
+## 1 NA NA
+## 2 5.250063 5.968927
+## ExpressionResults_Sample4.txt ExpressionResults_Sample5.txt
+## 1 NA NA
+## 2 6.868251 5.3671
+## ExpressionResults_Sample6.txt ExpressionResults_Sample7.txt
+## 1 NA NA
+## 2 5.189686 3.88293
+## ExpressionResults_Sample8.txt ExpressionResults_Sample9.txt
+## 1 NA NA
+## 2 5.329258 6.167451
+
+## Length Class Mode
+## 5001 character character
+
+## Length Class Mode
+## 5000 character character
+
+
+
+
++ +
+These exercises are about the factors and data frames sections of Introduction +to R.
+Exercise 1 - Factors
+## [1] DC1 DC1 DC1 NK NK Mono Mono DC2 NK
+## Levels: DC1 DC2 Mono NK
+## [1] DC1 DC1 Neu NK NK Mono Mono DC2 NK
+## Levels: DC1 DC2 Mono NK Neu
+CellType2 <- factor(c("DC1","DC1","DC1","NK","NK","Mono","Mono","DC2","NK"), levels = c("DC1", "DC2", "Mono", "NK", "Neu", "Bcell", "Tcell"))
+CellType2
## [1] DC1 DC1 DC1 NK NK Mono Mono DC2 NK
+## Levels: DC1 DC2 Mono NK Neu Bcell Tcell
+CellType2 <- c(CellType2, factor(c("Neu","Neu","Bcell","DC1"), levels = c("DC1", "DC2", "Mono", "NK", "Neu", "Bcell", "Tcell")))
+CellType2
## [1] DC1 DC1 DC1 NK NK Mono Mono DC2 NK Neu Neu Bcell
+## [13] DC1
+## Levels: DC1 DC2 Mono NK Neu Bcell Tcell
+## DC1 DC2 Mono NK Neu Bcell Tcell
+## 4 1 2 3 2 1 0
+## Bcell DC1 DC2 Mono Neu NK Tcell
+## 4 1 2 3 2 1 0
+Height <- factor(c("high", "low", "mid", "low", "mid", "low", "mid", "high", "mid", "high"),ordered=T,levels=c("low", "mid", "high"))
+Height
## [1] high low mid low mid low mid high mid high
+## Levels: low < mid < high
+## [1] high mid mid mid high mid high
+## Levels: low < mid < high
+newFactor <- factor(Height,ordered=T,levels=c("low", "mid", "high","veryHigh"))
+newFactor[length(newFactor)] <- "veryHigh"
+newFactor[newFactor > "mid"]
## [1] high high veryHigh
+## Levels: low < mid < high < veryHigh
+Exercise 2 - Data frames
+Annotation <- data.frame(geneNames=c("Gene_1", "Gene_2", "Gene_3","Gene_4","Gene_5"), ensembl=c("Ens001", "Ens003", "Ens006", "Ens007", "Ens010"),pathway=c("Glycolysis", "TGFb", "Glycolysis", "TGFb", "Glycolysis"),geneLengths=c(100, 3000, 200, 1000,1200))
+Annotation
## geneNames ensembl pathway geneLengths
+## 1 Gene_1 Ens001 Glycolysis 100
+## 2 Gene_2 Ens003 TGFb 3000
+## 3 Gene_3 Ens006 Glycolysis 200
+## 4 Gene_4 Ens007 TGFb 1000
+## 5 Gene_5 Ens010 Glycolysis 1200
+## geneNames ensembl pathway geneLengths
+## 4 Gene_4 Ens007 TGFb 1000
+## 5 Gene_5 Ens010 Glycolysis 1200
+## [1] "character"
+
+## [1] "character"
+
+## [1] "character"
+
+## [1] "numeric"
+
+## geneNames ensembl pathway geneLengths
+## 1 Gene_1 Ens001 Glycolysis 100
+## 2 Gene_2 Ens003 TGFb 3000
+## 3 Gene_3 Ens006 Glycolysis 200
+## 4 Gene_4 Ens007 TGFb 1000
+## 5 Gene_5 Ens010 Glycolysis 1200
+
+## [1] "factor"
+Sample1 <- data.frame(ensembl=c("Ens001", "Ens003", "Ens006","Ens010"),expression=c(1000, 3000, 10000,5000))
+Sample1
## ensembl expression
+## 1 Ens001 1000
+## 2 Ens003 3000
+## 3 Ens006 10000
+## 4 Ens010 5000
+Sample2 <- data.frame(ensembl=c("Ens001", "Ens003", "Ens006","Ens007","Ens010"),expression=c(1500, 1500, 17000,500,10000))
+Sample2
## ensembl expression
+## 1 Ens001 1500
+## 2 Ens003 1500
+## 3 Ens006 17000
+## 4 Ens007 500
+## 5 Ens010 10000
+AnnoSample1 <- merge(Annotation,Sample1,by.x=2,by.y=1,all=F)
+AnnoSample1And2 <- merge(AnnoSample1,Sample2,by=1,all=F)
+AnnoSample1And2
## ensembl geneNames pathway geneLengths expression.x expression.y
+## 1 Ens001 Gene_1 Glycolysis 100 1000 1500
+## 2 Ens003 Gene_2 TGFb 3000 3000 1500
+## 3 Ens006 Gene_3 Glycolysis 200 10000 17000
+## 4 Ens010 Gene_5 Glycolysis 1200 5000 10000
+AnnoSample1And2 <- AnnoSample1And2[order(AnnoSample1And2$geneLengths, decreasing = T),]
+AnnoSample1And2
## ensembl geneNames pathway geneLengths expression.x expression.y
+## 2 Ens003 Gene_2 TGFb 3000 3000 1500
+## 4 Ens010 Gene_5 Glycolysis 1200 5000 10000
+## 3 Ens006 Gene_3 Glycolysis 200 10000 17000
+## 1 Ens001 Gene_1 Glycolysis 100 1000 1500
+AnnoSample1And2$Sample1_lne <- AnnoSample1And2$expression.x/AnnoSample1And2$geneLengths
+AnnoSample1And2$Sample2_lne <- AnnoSample1And2$expression.y/AnnoSample1And2$geneLengths
+AnnoSample1And2
## ensembl geneNames pathway geneLengths expression.x expression.y
+## 2 Ens003 Gene_2 TGFb 3000 3000 1500
+## 4 Ens010 Gene_5 Glycolysis 1200 5000 10000
+## 3 Ens006 Gene_3 Glycolysis 200 10000 17000
+## 1 Ens001 Gene_1 Glycolysis 100 1000 1500
+## Sample1_lne Sample2_lne
+## 2 1.000000 0.500000
+## 4 4.166667 8.333333
+## 3 50.000000 85.000000
+## 1 10.000000 15.000000
+rownames(AnnoSample1And2) <- AnnoSample1And2$ensembl
+mean(c(AnnoSample1And2["Ens006","Sample1_lne"],AnnoSample1And2["Ens006","Sample2_lne"]))
## [1] 67.5
+log2FoldChange <- log2(AnnoSample1And2$Sample2_lne) - log2(AnnoSample1And2$Sample1_lne)
+names(log2FoldChange) <- AnnoSample1And2$geneNames
+log2FoldChange
## Gene_2 Gene_5 Gene_3 Gene_1
+## -1.0000000 1.0000000 0.7655347 0.5849625
+## [1] 1500
+
+
+
+
++ +
+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
+
+
+
+
++ +
+These exercises are about the conditions and loops sections of Introduction +to R.
+Exercise 1 - If Else
+## [1] "It's a negative number!"
+## [1] "It's not a negative number!"
+## [1] "It's a negative number!"
+## [1] "It's a negative number!"
+## [1] "It's zero"
+## [1] "It's a positive number!"
+Hint: The modulus operator may be useful here i.e. x%%2 returns +the remainder after the value of x is divided by 2.
+## [1] "1 is odd"
+## [1] "2 is even"
+– Using an ifelse() expression, create a factor from a vector of 1 to +40 where all numbers less than 10 are “small”,10 to 30 are “mid”,31 to +40 are “big”
+## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
+## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
+## [1] small small small small small small small small small mid mid mid
+## [13] mid mid mid mid mid mid mid mid mid mid mid mid
+## [25] mid mid mid mid mid mid big big big big big big
+## [37] big big big big
+## Levels: small < mid < big
+– Calculate the factorial (factorial of 3 = 3 * 2 * 1) of 10 using a +loop.
+## [1] 3628800
+– Adjusting your answer from before, what is the first number that +has a factorial greater than 1000.
+## [1] 7
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## [1] 5001
+## GeneNames Annotation.txt NA ExpressionResults_Sample1.txt
+## 1 GeneName Ensembl Pathway NA
+## 2 Gene_1 Ens_1001 DNA_Binding 3.448466
+## 3 Gene_10 Ens_10010 DNA_Binding 5.314180
+## ExpressionResults_Sample10.txt ExpressionResults_Sample2.txt
+## 1 NA NA
+## 2 7.665488 5.250063
+## 3 7.813501 5.361170
+## ExpressionResults_Sample3.txt ExpressionResults_Sample4.txt
+## 1 NA NA
+## 2 5.968927 6.868251
+## 3 5.305980 6.742855
+## ExpressionResults_Sample5.txt ExpressionResults_Sample6.txt
+## 1 NA NA
+## 2 5.367100 5.189686
+## 3 5.957786 6.293098
+## ExpressionResults_Sample7.txt ExpressionResults_Sample8.txt
+## 1 NA NA
+## 2 3.882930 5.329258
+## 3 7.361497 6.649428
+## ExpressionResults_Sample9.txt
+## 1 NA
+## 2 6.167451
+## 3 6.213910
+– Add annotation from Annotation.txt. How do the pathway information +for genes compare between expression table and annotation table.
+## GeneName Ensembl Pathway Annotation.txt NA
+## 1 GeneName <NA> <NA> Ensembl Pathway
+## 2 Gene_1 Ens_1001 DNA_Binding Ens_1001 DNA_Binding
+## ExpressionResults_Sample1.txt ExpressionResults_Sample10.txt
+## 1 NA NA
+## 2 3.448466 7.665488
+## ExpressionResults_Sample2.txt ExpressionResults_Sample3.txt
+## 1 NA NA
+## 2 5.250063 5.968927
+## ExpressionResults_Sample4.txt ExpressionResults_Sample5.txt
+## 1 NA NA
+## 2 6.868251 5.3671
+## ExpressionResults_Sample6.txt ExpressionResults_Sample7.txt
+## 1 NA NA
+## 2 5.189686 3.88293
+## ExpressionResults_Sample8.txt ExpressionResults_Sample9.txt
+## 1 NA NA
+## 2 5.329258 6.167451
+## Length Class Mode
+## 5001 character character
+## Length Class Mode
+## 5000 character character
+
+
+
+
++ +
+These exercises are about the factors and data frames sections of Introduction +to R.
+Exercise 1 - Factors
+## [1] DC1 DC1 DC1 NK NK Mono Mono DC2 NK
+## Levels: DC1 DC2 Mono NK
+## [1] DC1 DC1 Neu NK NK Mono Mono DC2 NK
+## Levels: DC1 DC2 Mono NK Neu
+## [1] DC1 DC1 DC1 NK NK Mono Mono DC2 NK
+## Levels: DC1 DC2 Mono NK Neu Bcell Tcell
+## [1] DC1 DC1 DC1 NK NK Mono Mono DC2 NK Neu Neu Bcell
+## [13] DC1
+## Levels: DC1 DC2 Mono NK Neu Bcell Tcell
+## DC1 DC2 Mono NK Neu Bcell Tcell
+## 4 1 2 3 2 1 0
+## Bcell DC1 DC2 Mono Neu NK Tcell
+## 4 1 2 3 2 1 0
+## [1] high low mid low mid low mid high mid high
+## Levels: low < mid < high
+## [1] high mid mid mid high mid high
+## Levels: low < mid < high
+## [1] high high veryHigh
+## Levels: low < mid < high < veryHigh
+Exercise 2 - Data frames
+## geneNames ensembl pathway geneLengths
+## 1 Gene_1 Ens001 Glycolysis 100
+## 2 Gene_2 Ens003 TGFb 3000
+## 3 Gene_3 Ens006 Glycolysis 200
+## 4 Gene_4 Ens007 TGFb 1000
+## 5 Gene_5 Ens010 Glycolysis 1200
+## geneNames ensembl pathway geneLengths
+## 4 Gene_4 Ens007 TGFb 1000
+## 5 Gene_5 Ens010 Glycolysis 1200
+## [1] "character"
+## [1] "character"
+## [1] "character"
+## [1] "numeric"
+## geneNames ensembl pathway geneLengths
+## 1 Gene_1 Ens001 Glycolysis 100
+## 2 Gene_2 Ens003 TGFb 3000
+## 3 Gene_3 Ens006 Glycolysis 200
+## 4 Gene_4 Ens007 TGFb 1000
+## 5 Gene_5 Ens010 Glycolysis 1200
+## [1] "factor"
+## ensembl expression
+## 1 Ens001 1000
+## 2 Ens003 3000
+## 3 Ens006 10000
+## 4 Ens010 5000
+## ensembl expression
+## 1 Ens001 1500
+## 2 Ens003 1500
+## 3 Ens006 17000
+## 4 Ens007 500
+## 5 Ens010 10000
+## ensembl geneNames pathway geneLengths expression.x expression.y
+## 1 Ens001 Gene_1 Glycolysis 100 1000 1500
+## 2 Ens003 Gene_2 TGFb 3000 3000 1500
+## 3 Ens006 Gene_3 Glycolysis 200 10000 17000
+## 4 Ens010 Gene_5 Glycolysis 1200 5000 10000
+## ensembl geneNames pathway geneLengths expression.x expression.y
+## 2 Ens003 Gene_2 TGFb 3000 3000 1500
+## 4 Ens010 Gene_5 Glycolysis 1200 5000 10000
+## 3 Ens006 Gene_3 Glycolysis 200 10000 17000
+## 1 Ens001 Gene_1 Glycolysis 100 1000 1500
+## ensembl geneNames pathway geneLengths expression.x expression.y
+## 2 Ens003 Gene_2 TGFb 3000 3000 1500
+## 4 Ens010 Gene_5 Glycolysis 1200 5000 10000
+## 3 Ens006 Gene_3 Glycolysis 200 10000 17000
+## 1 Ens001 Gene_1 Glycolysis 100 1000 1500
+## Sample1_lne Sample2_lne
+## 2 1.000000 0.500000
+## 4 4.166667 8.333333
+## 3 50.000000 85.000000
+## 1 10.000000 15.000000
+## [1] 67.5
+## Gene_2 Gene_5 Gene_3 Gene_1
+## -1.0000000 1.0000000 0.7655347 0.5849625
+## [1] 1500
+
+
+
+
++ +
+These exercises are about the matrices sections of Introduction +to R.
+Exercise 1
+## 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
+## 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
+## expression lne
+## Gene_2 3000 1
+## Gene_4 12000 12
+## 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.
+## expression geneLengths
+## 25000 4200
+
+
+
+
+## [1] "2024-12-12 02:00:20 UTC"
+## [1] "2024-12-12 18:12:15 UTC"
We can also use the arithmetic operations with our time objects.
-## [1] "2024-12-12 02:00:20 UTC"
+## [1] "2024-12-12 18:12:15 UTC"
-## [1] "2024-12-12 01:58:20 UTC"
+## [1] "2024-12-12 18:10:15 UTC"
-## Time difference of 0.1078458 secs
+## Time difference of 0.1084907 secs
We can also change the timezone by specifying a tz parameter
-## [1] "02 O'Clock AM Thursday on December 12th"
+## [1] "18 O'Clock PM Thursday on December 12th"
-## [1] "02 O'Clock AM Thursday on December 12th"
+## [1] "18 O'Clock PM Thursday on December 12th"
Most of the time we can convert more complex object back to our basic object types we are more familar with.
-## [1] "2024-12-12 02:00:20.799085"
+## [1] "2024-12-12 18:12:15.725485"
-## [1] 0.1078458
+## [1] 0.1084907
## Mean is -0.0473813821205204
-## [1] 2.066187
+## Mean is 0.260665894154018
+## [1] -0.2426098
These custom functions can also be utilized with apply.
-## [1] 2.06618698 0.17165772 -0.71309305 0.04463075 -1.24264432 0.21368949
-## [7] -0.82084457 0.89006732 -0.57284618 -0.46639582 -1.28333645 0.98048258
-## [13] 1.31683429 -0.36486729 -0.02032230 -0.68673550 0.13403087 1.02971875
-## [19] 1.13413397 -1.81034725
+## [1] -0.24260979 0.37093999 0.58712023 0.81503665 0.92783453 0.54524311
+## [7] -1.15824154 -0.38544962 0.08283829 0.73612828 -1.39531982 -0.48690779
+## [13] -2.08700758 1.41872341 -0.02320079 0.08587476 1.50147491 -1.67344992
+## [19] 0.86495743 -0.48398475