1. Simple ifelse statement Create the data frame ‘student.df’ with the data provided below: Use a simple ‘ifelse’ statement to add a new column ‘male.teen’ to the data frame. This is a boolean column, indicating T if the observation is a male younger than 20 years. 2. Double for loop Write a double for loop which prints
R Exercises – 61-70 – R String Manipulation | Working with ‘gsub’ and ‘regex’ | Regular Expressions in R
Required packages and datasets 1. ‘College’ dataset – Colleges in Texas a. Get familiar with the ‘college’ dataset and its row names. b. Get a vector with the college names (‘college.names’) which you will need in the further steps of this and the next exercises. c. Get a vector (‘texas.college’) which contains all colleges with ‘Texas’ in its name.
Required packages for the excises 1. ‘College’ dataset – Basic row manipulations a. Transform ‘College’ from ‘ISLR’ to data.table. Make sure to keep the University identifier. We will use this new data.table called ‘dtcollege’ throughout this block of exercises. b. Get familiar with the dataset and its variables. c. Extract rows 40 to 60 as a new data.table (‘mysubset’).
1. Simple time series plot on ‘non-ts’ data a. Get 200 random numbers and call the object ‘mydata’. Let’s set a seed of 14 for reproducibility. b. Get a time series plot without converting to class ‘ts’. c. Add ablines to the chart to indicate the horizontal boundaries of 0 and 1. 2. Working with ‘xts’ a. Get and load
1. Working with the ‘mtcars’ dataset a. Get a histogram of the ‘mpg’ values of ‘mtcars’. Which bin contains the most observations? b. Are there more automatic (0) or manual (1) transmission-type cars in the dataset? Hint: ‘mtcars’ has 32 observations. c. Get a scatter plot of ‘hp’ vs ‘weight’. 2. Working with the ‘iris’ dataset
1. Function ‘apply’ on a simple matrix: a. Get the following matrix of 5 rows and call it ‘mymatrix’ b. Get the mean of each row c. Get the mean of each column d. Sort the columns in ascending order 2. Using ‘lapply’ on a data.frame ‘mtcars’ a. Use three ‘apply’ family functions to get the minimum values
1. a. Write a function “myfun” of x to the power of its index position (x, x^2, x^3, …) b. Test the function with an x of 1:10 c. Enlarge the function “myfun” with a division through the index position (x, x^2 / 2, x^3 /3, …) 2. a. Write a simple moving average
Practicing is a crucial part of learning a new language. Statistical languages like R are no exception of that rule. Many of my students think the same and would love to see more exercises. Therefore, I decided to write an R exercise sheet for beginners and blog it over here. These R exercises are an
As most of you surely know, R has many exercise datasets already installed. That simply means, as soon as you installed R Base, which includes the library ‘datasets’, you have ample opportunity to explore R with real world data frames. For me as course content creator those datasets help tremendously, because with them I can