R runs on various operating systems, including Windows, macOS, and Linux, making it accessible to a broad audience.
Author: saqibkhan
-
Interactivity with Shiny
Shiny is an R package that enables users to build interactive web applications. This has made R popular for creating dashboards and data-driven web tools.
-
Data Visualization
R’s visualization capabilities are highly regarded, especially with packages like ggplot2, which allows users to create complex and aesthetically pleasing visualizations using a grammar of graphics.
-
How to aggregate data in R?
To aggregate data in R, we use the
aggregate()function. This function has the following essential parameters, in this order:x—the data frame to aggregate.by—a list of the factors to group by.FUN—an aggregate function to compute the summary statistics for each group (e.g.,mean,max,min,count,sum).
-
Statistical Powerhouse
R is particularly strong in statistical analysis. It includes a wide range of built-in functions for statistical tests, linear and nonlinear modeling, time-series analysis, and more.
-
What types of loops exist in R, and what is the syntax of each type?
1. For loop—iterates over a sequence the number of times equal to its length (unless the statements
breakand/ornextare used) and performs the same set of operations on each item of that sequence. This is the most common type of loops. The syntax of a for loop in R is the following:for (variable in sequence) {Powered By
}operations2. While loop—performs the same set of operations until a predefined logical condition (or several logical conditions) is met—unless the statements
breakand/ornextare used. Unlike for loops, we don’t know in advance the number of iterations a while loop is going to execute. Before running a while loop, we need to assign a variable (or several variables) and then update its value inside the loop body at each iteration. The syntax of a while loop in R is the following:variable assignment while (logical condition) {Powered By
}operations variable update3. Repeat loop—repeatedly performs the same set of operations until a predefined break condition (or several break conditions) is met. To introduce such a condition, a repeat loop has to contain an if-statement code block, which, in turn, has to include the
breakstatement in its body. Like while loops, we don’t know in advance the number of iterations a repeat loop is going to execute. The syntax of a repeat loop in R is the following:repeat {
}operations if(break condition) { break } -
What are the requirements for naming variables in R?
- A variable name can be a combination of letters, digits, dots, and underscores. It can’t contain any other symbols, including white spaces.
- A variable name must start with a letter or a dot.
- If a variable name starts with a dot, this dot can’t be followed by a digit.
- Reserved words in R (
TRUE,for,NULL, etc.) can’t be used as variable names. - Variable names are case-sensitive.
-
How to assign a value to a variable in R?
- Using the assignment operator
<-, e.g.,my_var <- 1—the most common way of assigning a value to a variable in R. - Using the equal operator
=, e.g.,my_var = 1—for assigning values to arguments inside a function definition. - Using the rightward assignment operator
->, e.g.,my_var -> 1—can be used in pipes. - Using the global assignment operators, either leftward (
<<-) or rightward (->>), e.g.,my_var <<- 1—for creating a global variable inside a function definition.
- Using the assignment operator
-
List some popular data visualization packages in R.
- ggplot2—the most popular R data visualization package allowing the creation of a wide variety of plots.
- Lattice—for displaying multivariate data as a tiled panel (trellis) of several plots.
- Plotly—for creating interactive, publication-quality charts.
- highcharter—for easy dynamic plotting, offers many flexible features, plugins, and themes; allows charting different R objects with one function.
- Leaflet—for creating interactive maps.
- ggvis—for creating interactive and highly customizable plots that can be accessed in any browser by using Shiny’s infrastructure.
- patchwork—for combining several plots, usually of various types, on the same graphic.
-
How to create a user-defined function in R?
To create a user-defined function in R, we use the keyword
functionand the following syntax:function_name <- function(parameters){Powered By
}function body- Function name—the name of the function object that will be used for calling the function after its definition.
- Function parameters—the variables separated with a comma and placed inside the parentheses that will be set to actual argument values each time we call the function.
- Function body—a chunk of code in the curly brackets containing the operations to be performed in a predefined order on the input arguments each time we call the function. Usually, the function body contains the
return()statement (or statements) that returns the function output, or theprint()statement (or statements) to print the output.
An example of a simple user-defined function in R:
my_function <- function(x, y){
}return(x + y)