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()
2- Create vector w as:
3- View vector w in the console: how does function rep() work ?
Play with the times argument.
correction
## [1] "miRNA" "miRNA" "miRNA" "mRNA" "mRNA" "mRNA" "mRNA"
## [1] "miRNA" "miRNA" "miRNA" "miRNA" "miRNA" "miRNA" "miRNA" "miRNA"
## [9] "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
## [1] "mRNA" "mRNA"
## [1] "mRNA" "mRNA"
6- Now type w[grep(pattern=“RNA”, w)] and w[w == “RNA”]
Is there a difference between the two outputs?
correction
## [1] "miRNA" "miRNA" "miRNA" "mRNA" "mRNA"
## character(0)
What is the difference between == and grep ?
correction
= looks for exact matches.
grep looks for patterns.
7- Create vector g as:
How many elements do w and g contain?
8- Do vectors w and g have the same length? Use the function identical() to check this.
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
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.
2- Count how many microRNAs and mRNAs there are in g based on the column names.
3- Create vector tt as:
How many characters does tt contain? Use nchar().
4- Remove “Introduction to R” from tt.
You can try with either substr() or gsub()
correction
## [1] "R course"
## [1] " course"