R PROGRAMMING
1) WHAT IS R PROGRAMMING ?
R is an open-source programming language designed for statistical computing and data analysis. It provides a wide range of tools for data manipulation, calculation, and graphical display. R is widely used in academic research, data science, and industries requiring statistical analysis. It supports a vast collection of packages that extend its functionality in various fields.[www.w3schools.com]
![]() |
R PROGRAMMING |
R's strong suit is its ability to create high-quality data visualizations. It excels in tasks like regression, hypothesis testing, and machine learning. The language is known for its flexibility and extensive user community. R runs on multiple platforms, including Windows, Mac, and Linux.
2) HOW TO INSTALL R :
Here’s how to install R on your system: [YOUTUBE VIDEO LINK]
Windows:
1. Go to the official R website: [https://cran.r-project.org/](https://cran.r-project.org/).
2. Click on **"Download R for Windows."**
3. Click **"base"** under the "Subdirectories" section.
4. Click **"Download R x.x.x for Windows"** to get the latest version.
5. Run the downloaded installer and follow the instructions.
6. After installation, R is ready to use.
macOS:
1. Visit the same website: [https://cran.r-project.org/](https://cran.r-project.org/).
2. Click on **"Download R for macOS."**
3. Select the latest version and download the package.
4. Open the downloaded file and follow the on-screen instructions to install.
Linux:
1. Open your terminal.
2. Use the following commands to install R:
Ubuntu/Debian:
sudo apt update
sudo apt install r-base
Fedora:
sudo dnf install R
3. Once installed, you can launch R by typing `R` in your terminal.
3) R SYNTAX:
R syntax is relatively simple and similar to other programming languages. Below are the basic components of R syntax:
1. Variables:
Variables are used to store data, and the assignment operator `<-` is used to assign values.
x <- 10
y <- "Hello, R"
2. Data Types:
R has various data types like numeric, character, logical, etc.
num <- 5.5 # Numeric
str <- "Text" # Character
bool <- TRUE # Logical
3. Functions:
Functions are used to perform tasks in R.
print("Hello World")
sum(5, 10)
4. Vectors:
Vectors are one-dimensional arrays of data.
vec <- c(1, 2, 3, 4, 5)
# c() function creates a vector
5. Conditional Statements:
`if`, `else` are used for conditional execution.
if (x > 5) {
print("x is greater than 5")
} else {
print("x is less than or equal to 5")
}
6. Loops:
`for` and `while` loops are used for iteration.
for (i in 1:5) {
print(i)
}
7. Commenting:
Single-line comments start with `#`.
# This is a comment
This basic syntax allows you to perform various operations in R.
4) R VARIABLES:
In R, variables are used to store values such as numbers, strings, or complex data structures like vectors, lists, or data frames. Here's an overview of R variables:
1. Variable Assignment:
Variables are assigned using the `<-` operator (preferred) or the `=` operator.
x <- 10 # Assigning 10 to x
y = "R Programming" # Assigning a string to y
2. Variable Naming Rules:
Names can include letters, numbers, underscores (`_`), and periods (`.`).
Variable names **cannot** start with a number.
R is case-sensitive (e.g., `x` and `X` are different).
my_var <- 25 # Valid
var2 <- "Data" # Valid
2var <- 10 # Invalid (cannot start with a number)
3. Data Types in Variables:
Numeric: Stores numbers (integer or floating-point).
Character: Stores strings.
Logical: Stores `TRUE` or `FALSE`.
Complex: Stores complex numbers (e.g., `1+2i`).
num <- 100 # Numeric variable
text <- "R" # Character variable
flag <- TRUE # Logical variable
comp <- 3 + 4i # Complex variable
4. Checking Variable Type:
You can check the type of a variable using the `class()` function.
class(num) # Returns "numeric"
class(text) # Returns "character"
5. Displaying Variable Values:
Simply typing the variable name or using the `print()` function displays the value.
x <- 50
print(x) # Outputs 50
5) R DATA TYPES:
R has several fundamental data types that allow it to handle different kinds of data. Here are the primary data types in R:
1. Numeric:
This type is used for numbers, both integers and floating-point numbers.
num1 <- 5 # Numeric (default is double)
num2 <- 5.75 # Numeric (floating point)
2. Integer:
To explicitly define an integer, append `L` to the number.
int_var <- 10L # Integer
3. Character (String):
Character data stores text or string values.
char_var <- "Hello, World!" # Character
4. Logical (Boolean):
Logical type represents `TRUE` or `FALSE` values.
is_true <- TRUE # Logical
is_false <- FALSE # Logical
5. Complex:
This data type is used to represent complex numbers with real and imaginary parts.
comp_var <- 2 + 3i # Complex number
6. Factor:
Factors are used for categorical data and store values as levels.
factor_var <- factor(c("Low", "Medium", "High"))
7. Raw:
The raw data type stores raw bytes.
raw_var <- charToRaw("R") # Raw data
8. Special Values:
NA: Missing data.
NaN: Not a number (e.g., `0/0`).
Inf / -Inf: Positive or negative infinity.
missing_value <- NA # Missing value
not_a_number <- NaN # Not a number
positive_inf <- Inf # Positive infinity
0 Comments