These exercises are about the vector sections of Introduction to R.

Exercise 1

c(1,2,3,4,5)
## [1] 1 2 3 4 5
x <- 1:100
x
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
x*2
##   [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36
##  [19]  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72
##  [37]  74  76  78  80  82  84  86  88  90  92  94  96  98 100 102 104 106 108
##  [55] 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144
##  [73] 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180
##  [91] 182 184 186 188 190 192 194 196 198 200
x <- seq(0,20,5)
x
## [1]  0  5 10 15 20
?rep
## Help on topic 'rep' was found in the following packages:
## 
##   Package               Library
##   base                  /usr/local/lib/R/library
##   S4Vectors             /usr/local/lib/R/host-site-library
## 
## 
## Using the first match ...
x <- c(rep(1,2),rep(2,2),rep(3,2))
x
## [1] 1 1 2 2 3 3
x <- c(rep(1,2),seq(5,9,2),10)
x
## [1]  1  1  5  7  9 10

Exercise 2

x <- 1:10
x
##  [1]  1  2  3  4  5  6  7  8  9 10
y <- x[-c(1,length(x))]
y
## [1] 2 3 4 5 6 7 8 9
y <- x[-c(2,5)]
y
## [1]  1  3  4  6  7  8  9 10
y <- sqrt(x[6:7])
y
## [1] 2.449490 2.645751
y <- x[seq(1,10,2)]
y
## [1] 1 3 5 7 9

Exercise 3

?paste
## Help on topic 'paste' was found in the following packages:
## 
##   Package               Library
##   base                  /usr/local/lib/R/library
##   BiocGenerics          /usr/local/lib/R/host-site-library
## 
## 
## Using the first match ...
paste("A","B")
## [1] "A B"
paste("A","B", sep="_")
## [1] "A_B"

Exercise 4

my_genes <- c("PKM", "ADPRH", "TDG", "ATP4A", "SLC6A4", "CAPN3", "TDG", "ATP1A2","PKM")
my_genes <- unique(my_genes)
my_genes 
## [1] "PKM"    "ADPRH"  "TDG"    "ATP4A"  "SLC6A4" "CAPN3"  "ATP1A2"
my_genes_of_interest <- c("SLC6A4", "CAPN3", "TDG", "ATP1A2", "IMPA1", "PDXK")
idx <- my_genes %in% my_genes_of_interest
idx
## [1] FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
my_genes[idx]
## [1] "TDG"    "SLC6A4" "CAPN3"  "ATP1A2"
my_genes_of_interest <- c("SLC6A4", "CAPN3", "TDG", "ATP1A2", "IMPA1", "PDXK")
idx <- my_genes %in% my_genes_of_interest
idx
## [1] FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
my_genes[idx]
## [1] "TDG"    "SLC6A4" "CAPN3"  "ATP1A2"
my_genes <- c("SMC1", "SMC3", "SCC1", "SCC3", "RAD21", "NIPBL", "SMC2", "SMC4","CAPH","CAPD3")
my_genes[grepl("SMC",my_genes)]
## [1] "SMC1" "SMC3" "SMC2" "SMC4"

Exercise 5

geneNames <- c("Gene_1", "Gene_2", "Gene_3","Gene_4")
expression <- c(1000, 3000, 10000, 12000)
geneLengths <- c(100, 3000, 200, 1000)
names(expression) <- geneNames
names(geneLengths) <- geneNames
expression
## Gene_1 Gene_2 Gene_3 Gene_4 
##   1000   3000  10000  12000
geneLengths
## Gene_1 Gene_2 Gene_3 Gene_4 
##    100   3000    200   1000
names(geneLengths[geneLengths == max(geneLengths)])
## [1] "Gene_2"
names(geneLengths[which.max(geneLengths)])
## [1] "Gene_2"
geneNames[geneLengths > 100 & expression > 10000]
## [1] "Gene_4"

Bonus Questions

lne <- expression/geneLengths
lne
## Gene_1 Gene_2 Gene_3 Gene_4 
##     10      1     50     12
geneNames[lne > mean(lne)]
## [1] "Gene_3"