## 9.3 Exercise 3. Character vector manipulation

### 9.3.1 Exercise 3a.

Create the script “exercise3.R” and save it to the “Rcourse/Module1” directory: you will save all the commands of exercise 3 in that script.
Remember you can comment the code using #.

1- Go to Rcourse/Module1 First check where you currently are with getwd(); then go to Rcourse/Module1 with setwd()

correction

``````getwd()
setwd("Rcourse/Module1")
setwd("~/Rcourse/Module1")``````

2- Create vector w as:

``w <- rep(x=c("miRNA", "mRNA"), times=c(3, 2))``

3- View vector w in the console: how does function rep() work ?
Play with the times argument.

correction

``rep(x=c("miRNA", "mRNA"), times=c(3, 4))``
``##  "miRNA" "miRNA" "miRNA" "mRNA"  "mRNA"  "mRNA"  "mRNA"``
``rep(x=c("miRNA", "mRNA"), times=c(10, 2))``
``````##   "miRNA" "miRNA" "miRNA" "miRNA" "miRNA" "miRNA" "miRNA" "miRNA"
##   "miRNA" "miRNA" "mRNA"  "mRNA"``````

4- What is the output of table(w) ? What does the table function do ?

5- Type w[grep(pattern=“mRNA”, x=w)] and w[w == “mRNA”]
Is there a difference between the two outputs?

correction

``w[grep(pattern="mRNA", w)]``
``##  "mRNA" "mRNA"``
``w[w == "mRNA"]``
``##  "mRNA" "mRNA"``
``# no difference between the outputs``

6- Now type w[grep(pattern=“RNA”, w)] and w[w == “RNA”]
Is there a difference between the two outputs?

correction

``w[grep(pattern="RNA", w)]``
``##  "miRNA" "miRNA" "miRNA" "mRNA"  "mRNA"``
``w[w == "RNA"]``
``## character(0)``
``# grep outputs 5 values but == outputs none``

What is the difference between == and grep ?

correction

= looks for exact matches.
grep looks for patterns.

7- Create vector g as:

``g <- c("hsa-let-7a", "hsa-mir-1", "CLC", "DKK1", "LPA")``

How many elements do w and g contain?

correction

``length(w); length(g)``
``##  5``
``##  5``

8- Do vectors w and g have the same length? Use the function identical() to check this.

correction

``identical(x=length(w), y=length(g))``
``##  TRUE``

9- Name the elements of g using the elements of w.
(i.e. the names of each element of g will be the elements of w).

correction

``names(g) <- w``

If you have time, continue with Exercise 3b below.

### 9.3.2 Exercise 3b.

1- Use the sub() function to replace miRNA with microRNA in the names of g.

correction

``names(g) <- sub(pattern="miRNA", replacement="microRNA", x=names(g))``

2- Count how many microRNAs and mRNAs there are in g based on the column names.

correction

``table(names(g))``
``````##
## microRNA     mRNA
##        3        2``````

3- Create vector tt as:

``tt <- "Introduction to R course"``

How many characters does tt contain? Use nchar().

correction

``nchar(tt)``
``##  24``

4- Remove “Introduction to R” from tt.
You can try with either substr() or gsub()

correction

``substr(x=tt, start=17, stop=nchar(tt))``
``##  "R course"``
``gsub(pattern="Introduction to R", replacement="", x=tt)``
``##  " course"``