## Exercise 3. Character vector manipulation. # Create the script "exercise3.R" and save it to the "Rintro/day1" directory: # you will save all the commands of exercise 3 in that script. # 1. Go to Rintro/day1 # First check where you currently are with getwd(); # then go to Rintro/day1 with setwd() getwd() setwd("Rintro/day1") setwd("/users/bi/sbonnin/Rintro/day1") # 2. Create vector w: w <- rep(x=c("miRNA", "mRNA"), times=c(3, 2)) # 3. View vector w in the console: what is function rep() doing? # Play with the ???times??? argument. rep(x=c("miRNA", "mRNA"), times=c(3, 4)) rep(x=c("miRNA", "mRNA"), times=c(10, 2)) # 4. What does table(w) outputs? table(w) # 5. Type w[grep(pattern="mRNA", x=w)] and w[w == "mRNA"] # Is there a difference between the two outputs? w[grep(pattern="mRNA", w)] w[w == "mRNA"] # 6. Now type w[grep(pattern="RNA", w)] and w[w == "RNA"] # Is there a difference between the two outputs? w[grep(pattern="RNA", w)] w[w == "RNA"] # 7. Create vector g: g <- c("hsa-let-7a", "hsa-mir-1", "CLC", "DKK1", "LPA") # How many elements do w and g contain? length(w); length(g) # 8. Use the function identical() to check whether # the lengths of w and g are the same. identical(x=length(w), y=length(g)) # 9. Name the elements of g using w # (i.e. the names of each element of g will be the elements of w). names(g) <- w ## Exercise 3b. Optional # 1. Use the sub() function to replace ???miRNA??? with ???microRNA??? # in the names of g. 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. table(names(g)) # 3. Create vector tt: tt <- "Introduction to R course" # How many characters does tt contain? nchar() # 4. Remove ???Introduction to R ??? from tt. # You can try with substr() or gsub() substr(x=tt, start=17, stop=nchar(tt)) gsub(pattern="Introduction to R ", x=tt)