Below is a number of exercises to play around with data structures in R. Some of them can be "solved" using the material we have just discussed in the last tutorial session. For others, explore! Ways to learn and find out about useful R commands are using the inbuilt help function, but also using internet search to find tutorials and help from the R community.
Exercises I
- Calculate the square root of 719.
- Create a new variable "b" and assign it the value 178.0. Print b.
- Convert the data type of "b" from the previous exercise to character. Print b.
- Create a vector of numbers from 1 to 6. Find out its class. Create a vector with the "mixed" elements 1,2, "a" and "b". Find out its class.
- Initialize a character vector of size 30 and assign "a" to the first element of the vector.
- Create a vector with some of your friends names. Determine the length of that vector. Get the second and third friends of that vector. Sort that vector (explore the use of the "sort" and "order" functions). Sort that vector in reverse order.
- Explore the use of "rep" and "seq" to initialize vectors and generate the vector ('a','a',1,2,3,4,5,7,9,11) using these commands.
- Generate the vector (1, 2, NA, 4) and remove missing values from it.
- Generate a vector that contains a sample of 100 random numbers picked from 1-100 with replacement. Set the seed of the random number generator to 100 before sampling. Check if the sample contains any number twice. Calculate the mean value of this sample. Explore how the standard deviation of the sample changes with sample size!
- R has some inbuilt data frames that are already populated with data that you can use to explore R functions. Explore the data frame "iris", i.e. figure out how many rows/columns it has, the class of all columns, get row and column names.
- Get the last two rows in the last two columns of the iris data frame. Get rows with Sepal.Width > 3.5 from irids. Get the rows with the "versicolor" species from iris (use the "subset" command)