40 Essential R Questions to Test Your Data Science Skills

R is increasingly becoming the go-to tool for data science, and especially machine learning (ML) and artificial intelligence (AI) applications. Its extensive ecosystem of packages, stellar performance, and active community make it ideal for all stages of the data science lifecycle.

In fact, the 2019 Kaggle Machine Learning and Data Science Survey found that 75% of data scientists use R. R‘s popularity for analytics, data science and ML has grown rapidly in recent years:

R popularity growth

Source: IEEE Spectrum, The Top Programming Languages 2019

To assess your R data science skills, we compiled these 40 questions originally featured in DataFest 2017. They cover essential skills like data manipulation, visualization, string processing, and basic statistics.

Let‘s walk through the key concepts, best practices, and worked solutions for each question category. Whether you‘re a beginner or a more advanced R user, mastering these fundamentals will set you up for success in using R for data science and AI/ML.

Importing and Manipulating Data

Data import and wrangling are critical first steps in any analysis or ML project. R offers a variety of functions for reading data from different formats and transforming it for analysis. Key packages to know include:

  • readr – for reading rectangular data like CSV files
  • readxl – for reading Excel files
  • jsonlite – for reading JSON data
  • xml2 – for reading XML data
  • httr – for making HTTP requests to APIs
  • DBI – for connecting to databases

Some best practices when importing data:

  • Check the structure of the raw file before importing
  • Specify column types when reading data for better performance
  • Set stringsAsFactors=FALSE to avoid string to factor conversion
  • Handle missing values explicitly with na argument

Once you‘ve loaded the data, packages like dplyr and data.table allow you to efficiently manipulate it. Some key dplyr functions:

Function Description
filter() Subset rows matching criteria
arrange() Sort rows by values
select() Subset columns by name
mutate() Create new columns
summarize() Reduce rows to a single value

For example, to find the top 5 highest scoring students:

students %>%
  arrange(desc(score)) %>%
  head(5)

And to calculate average score by grade:

students %>%
  group_by(grade) %>%
  summarize(avg_score = mean(score))  

Data Visualization

R has some of the most powerful data visualization capabilities of any language. The ggplot2 package is the most popular tool for creating professional graphics in R. It‘s based on a "grammar of graphics" that allows you to expressively build plots layer-by-layer.

The key components of a ggplot2 graphic:

  • data – the dataset to plot
  • aes – aesthetic mappings of data to visual properties
  • geom_* – the geometric object to use, like geom_point() or geom_bar()
  • stats – statistical transformations to apply, like a regression line
  • scales – what scales to use for each aesthetic mapping
  • facet_* – how to split the data into subplots

Some examples of ggplot2 in action:

# Scatter plot with regression line
ggplot(data=mtcars, aes(x=hp, y=mpg)) +
  geom_point() +
  geom_smooth(method=‘lm‘)

ggplot2 scatter plot

# Stacked bar chart 
ggplot(diamonds, aes(clarity, fill=cut)) +
  geom_bar(position="fill")

ggplot2 stacked bar chart

Images generated with ggplot2 examples

String Processing

Working with text data is increasingly important for NLP and text mining ML applications. Some common string manipulation tasks in R:

Task Base R stringr
Concatenate paste() str_c()
Substring substr() str_sub()
Split strsplit() str_split()
Replace gsub() str_replace_all()
Trim whitespace trimws() str_trim()

For example, to parse out the area code from phone numbers:

phone_numbers <- c("(123) 456-7890", "(345) 678-9101")

str_sub(phone_numbers, start=2, end=4)

The stringr package provides more consistent and feature-rich string functions compared to base R. Regular expressions give you even more power to match patterns in strings.

Basic Statistics

R was born as a statistical programming language and it excels at statistical computing. Key functions to calculate summary statistics:

x <- c(12, 7, 3, 4.2, 18, 2, 54, -21, 8, -5)

mean(x)
median(x) 
sd(x)
min(x)
max(x)
quantile(x)

For categorical data, use table() to tabulate frequencies:

y <- c("red", "green", "blue", "red", "red", "green")

table(y)

And prop.table() to calculate proportions:

prop.table(table(y))

R also offers probability distribution functions and statistical tests:

# Probability of values ≤ 1.7 from a normal distribution
pnorm(1.7, mean=1, sd=2) 

# Two-sample t-test
t.test(y1, y2, var.equal=TRUE)

Machine Learning

R offers a robust framework for machine learning with the caret package. It provides a consistent interface to train and evaluate models across a wide range of ML algorithms.

Some of the most popular ML packages in R:

  • glmnet – Lasso and elastic-net regularized generalized linear models
  • randomForest – Breiman and Cutler‘s random forests for classification and regression
  • xgboost – Extreme Gradient Boosting (tree) models
  • e1071 – Support Vector Machines
  • keras – R interface to Keras deep learning library

With caret, you can easily compare models, optimize hyperparameters, and evaluate performance. A simple example of training and testing a random forest model:

library(caret)

# Split data into train/test sets
trainIndex <- createDataPartition(iris$Species, p=0.8, list=FALSE)
irisTrain <- iris[trainIndex,]
irisTest <- iris[-trainIndex,]

# Fit a random forest model
model <- train(Species ~ ., data=irisTrain, method="rf")

# Make predictions on test set
predictions <- predict(model, newdata=irisTest)
confusionMatrix(predictions, irisTest$Species)

The ability to prototype and compare models so quickly makes R a powerful tool for machine learning.

Conclusion

Assessing your skills with these 40 questions is a great starting point for using R for data science and AI/ML. However, the rich package ecosystem and active community mean there‘s always more to learn.

Some key resources for taking your R machine learning skills to the next level:

No matter what industry you work in, being able to manipulate, visualize, and model data in R is an incredibly valuable skill. Keep challenging yourself with new datasets and algorithms. Participate in Kaggle competitions. Contribute to open-source packages. Start a data science blog to share your projects.

The more you practice and learn from the community, the more prepared you‘ll be to tackle cutting-edge data science and AI challenges with R. Stay curious and enjoy putting your skills to work!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts