Exercises with vectors
- Create the following vectors: 1, 1, 3, 4, 5 and 2, 2, 5, 4, 1
- Find the minimum of both vectors
- Find the common minimum of the vectors
- Summarize the vectors element-wise and all elements. 4 create the element-wise squere root of the element-wise sum of the vectors.
- order both vectors in decreasing order
- find the elements that are duplicated in the vectors.
- find out which element of vector one is in vector 2.
- Create one vector with 100 random numbers, between 1 and 100, with the possibility to repeat (hint: sample function)
- find out, how many elements are equal to three.
- do it again - random numbers
- do it with running the set.seed(23)
- change all the elements that are equal to three to 4. check your results.
- create named vectors of the two first vecotrs. Order the second one as the first, based on the names (match)
- combine the two vectors
- Is there any element of vector two that is larger than the respective element of vector 1?
- Is there any element of vector two that is larger than the the largest element of vector 1?
Exercises with data frames
- load the iris dataset (data)
- exctract the Petal.Lenght column as a vector. Do it by column name and column index as well.
- create a data frame with the columns Sepal.Width, Sepal.Length and Species colums.
- Get the maximum Petal.With for the Species setosa.
- Get the second element of the Sepal.Width column
- How many setosa are there with the Petal.Width of 0.2
Exercises with regular expressions
dataset <- data.frame(Patient.ID=c("normal_01", "normal_02", "normal_03", "tumor_01", "tumor_02", "tumor_02"),
Sentrix.position=c("A01B01", "A01B02", "A016A01", "B02A02", "C01D02", "C02C01"), Treatment=c("Treated", "Treated", "Not treated", "Treated", "Treated", "Not treated"), value=c(3.25, 3.67, 4.26, 6.24, 5.78, 7.32), row.names = c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5", "Sample6"))
dataset
## Patient.ID Sentrix.position Treatment value
## Sample1 normal_01 A01B01 Treated 3.25
## Sample2 normal_02 A01B02 Treated 3.67
## Sample3 normal_03 A016A01 Not treated 4.26
## Sample4 tumor_01 B02A02 Treated 6.24
## Sample5 tumor_02 C01D02 Treated 5.78
## Sample6 tumor_02 C02C01 Not treated 7.32
- Create a column with sample type (tumor or normal)
- table treatment versus sample type
- add an "_" to the sample names: sample_3
- summarize all values that are coming from normal samples
- change all “A”s in the Sentrix.position column to “E”s.
- change all “E”s back to “A”s, if they appear second. Do it as generalized as possible.
Exercises with factors
- check if there are any factors in dataset.
- Turn the sample type column into factors.
- Add an “unknown” level
- Order the levels, so the first is “tumor”.
- Order them according the the mean of value in decreasing order.
Lists
- Create a list with 5 elements, each different class.
- Create a list with one vector, one list, one matrix and one number. Name the list elements. Access the third element by name.
- Delete the second element of the above list.
- Add a data frame to the end of the list. Access the 3rd row, 2nd column element of that data frame.
- Create a list with two elements, where each element has two sublists.