< All Topics

What is R?

R is a programming language and environment for statistical computing and graphics. It is widely used in data analysis, statistical modeling, and data visualization. Here are some basics of R programming to help you get started:

1. Installation and Setup:

2. Basic R Syntax:

  • R is an interpreted language, and you can use it interactively or write scripts.
  • Arithmetic Operations:
    • Addition 5 + 3
    • Subtraction 7 – 2
    • Multiplication 4 * 6
    • Division 10 / 2
  • Assigning Values to Variables:
    • Assigning a value to a variable x <- 10 # Display the value of x print(x)

3. Data Types:

  • Numeric:
    • numeric_var <- 5.2
  • Integer:
    • integer_var <- 3L
  • Character/String:
    • string_var <- "Hello, R!"
  • Logical/Boolean:
    • logical_var <- TRUE

4. Vectors and Data Structures:

  • Vectors:
    • # Creating a numeric vector numeric_vector <- c(1, 2, 3, 4, 5)
    • # Creating a character vector character_vector <- c("apple", "orange", "banana")
  • Matrices:
    • # Creating a matrix matrix_data <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
  • Lists:
    • # Creating a list my_list <- list(numeric_vector, character_vector, matrix_data)

5. Data Frames:

  • Data Frames:
    • Creating a data frame my_data <- data.frame( Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 22), Grade = c("A", "B", "C") )

6. Control Structures:

  • Conditional Statements (if-else):
    • if (condition) { # code to be executed if condition is TRUE } else { # code to be executed if condition is FALSE }
  • Loops (for, while):
    • For loop for (i in 1:5) { print(i) } # While loop i <- 1 while (i <= 5) { print(i) i <- i + 1 }

7. Functions:

  • Defining Functions:
    • Function definition my_sum <- function(a, b) { result <- a + b return(result) } # Function call my_sum(3, 4)

8. Data Analysis and Visualization:

  • R has a rich ecosystem of packages for data analysis and visualization, including:
    • dplyr: for data manipulation.
    • ggplot2: for data visualization.

9. Help and Documentation:

  • Use the help() function or ? before a function name for documentation.
    • help(mean) # or ?mean
  • R also has built-in datasets for practice. For example, you can explore the iris dataset:
    • head(iris)

10. Learning Resources:

  • The R documentation and help resources are comprehensive and can be accessed online.

This is just a brief introduction to the basics of R programming. As you progress, you can explore more advanced topics, statistical modeling, machine learning, and specialized libraries in R.

Table of Contents