R Program Commands - GeeksforGeeks (2024)

Last Updated : 11 Jun, 2024

Improve

R is a powerful programming language and environment designed for statistical computing and data analysis. It is widely used by statisticians, data scientists, and researchers for its extensive capabilities in handling data, performing statistical analysis, and creating visualizations.

Table of Content

  • Overview of R Commands
  • Data Visualizations commands
  • Statistical Analysis commands
  • Data Import and Export commands
  • Control Structures and Conditionals commands
  • Data Structures in R

Overview of R Commands

The following R commands provide an overview of different application areas in R programming. Depending on our specific needs and projects, we can pick and match the commands that suits.

1. Reading and Writing Commands

Reading and writing data are fundamental tasks in data analysis and manipulation. In R, several functions and packages can help you handle different types of data sources.

  • read.csv(): Read data from a CSV file.
  • write.csv(): Write data to a CSV file.

2. Dataframe Operations Commands

Dataframe operations in R are essential for data manipulation and analysis. Here are some common operations you might perform on data frames using base R and the dplyr package, which is part of the tidyverse collection.

  • data.frame(): Create a data frame.
  • subset(): Filter data based on specific conditions.
  • merge(): Merge data from different data frames.
  • aggregate(): Aggregate data based on specific criteria.
  • transform(): Create new variables in a data frame.
  • sort(): Sort vectors or data frames.
  • unique(): Identify unique values in a vector or column.

3. Applying Functions Commands

Applying functions to data frames is a powerful technique in R for data transformation and analysis. Here are various ways to apply functions to data frames.

  • apply(): Apply a function to rows or columns of matrices or data frames.
  • lapply(), sapply(), mapply(): Apply functions to lists or vectors.

4. Using dplyr for Data Manipulation

dplyr is a powerful package in R designed to make data manipulation easy and intuitive. It provides a set of verbs that allow you to solve the most common data manipulation challenges:.

  • dplyr::filter(): Filter data in data frames.
  • dplyr::mutate(): Create new variables in data frames.
  • dplyr::select(): Select specific columns from a data frame.
  • dplyr::summarize(): Summarize data by applying functions.

Data Visualizations Commands

Data visualization is a critical part of data analysis, and R offers powerful libraries like ggplot2 for creating various types of visualizations. Below are some examples of common visualizations and the commands to create them using ggplot2.

1. Base R Plotting Functions

In R, base plotting functions provide a straightforward way to create a wide range of plots. Here are some commonly used base R plotting functions along with examples of how to use them.

  • plot(): Create scatter plots and other basic plot types.
  • hist(): Create histograms.
  • barplot(): Create bar charts.
  • boxplot(): Create box plots.
  • ggplot2::ggplot(): Create sophisticated and customizable visualizations.

2. Specialized Plots

Specialized plots cater to specific data visualization needs, offering more advanced and tailored representations. Here are some examples of specialized plots in R along with their corresponding packages and usage:

  • qqnorm(), qqline(): Create quantile-quantile plots.
  • acf(): Display autocorrelation functions.
  • density(): Display density functions and histograms.
  • heatmap(): Create heat maps.

Statistical Analysis Commands

Statistical analysis in R involves a wide range of techniques and commands. Here are some common statistical analysis commands along with examples of how to use them.

1. Descriptive Statistics

To compute descriptive statistics such as mean, median, standard deviation, and quartiles, you can use the summary and quantile functions:

  • summary(): Get a summary of data, including statistical metrics.
  • mean(), median(), sd(): Calculate mean, median, and standard deviation.

2. Hypothesis Testing

Hypothesis testing is a fundamental concept in statistics used to make inferences about a population based on sample data.

  • t.test(): Perform T-tests for hypothesis testing.
  • anova(): Perform analysis of variance (ANOVA).
  • chi-sq.test(): Perform chi-square tests.

3. Regression and Correlation

Regression and correlation are statistical techniques used to analyze the relationship between variables.

  • lm(): Perform linear regressions.
  • cor(): Calculate correlation coefficients between variables.

Data Import and Export Commands

Data import and export are essential tasks in R for working with external data sources. Here’s how you can import and export data using R.

1. R Data Objects

In R, there are several data objects commonly used for storing and working with data. Here are some of the most frequently used.

  • readRDS(), saveRDS(): Read and save R data objects.

2. Reading and Writing Various Formats

Reading and writing data in various formats is a common task in R. Here’s how you can handle different file formats:

  • read.table(): Read data from a file.
  • write.table(): Write data to a file.

3. Excel Files

Working with Excel files in R is a common task, and there are multiple packages available for reading and writing Excel files.

  • readxl::read_excel(): Read data from Excel files.
  • writexl::write_xlsx(): Write data to Excel files.

Control Structures and Conditionals Commands

Control structures and conditionals in R allow you to control the flow of execution in your code based on certain conditions.

1. Conditional Statements

Conditional statements in R allow you to execute specific code blocks based on whether certain conditions are true or false.

  • ifelse(): Perform condition evaluations and conditional expressions.

2. Loops

Loops in R allow you to execute a block of code repeatedly. Here are some common types of loops:

  • for(): Loop over a sequence.
  • while(): Perform while loops.
  • repeat: Execute a loop indefinitely until a condition is met.

Data Structures in R

R provides several data structures to store and manage data efficiently.

1.Vectors

  • A sequence of elements of the same type.
  • Created using the c() function.
R
num_vec <- c(1, 2, 3, 4, 5)char_vec <- c("apple", "banana", "cherry")print(num_vec)print(char_vec)print(class(num_vec))print(class(char_vec))

Output:

[1] 1 2 3 4 5[1] "apple" "banana" "cherry"[1] "numeric"[1] "character"

2. Matrices

  • Two-dimensional data structure with rows and columns, where all elements are of the same type.
  • Created using the matrix() function.
R
mat <- matrix(1:9, nrow = 3, ncol = 3)print(mat)print(class(mat))

Output:

 [,1] [,2] [,3][1,] 1 4 7[2,] 2 5 8[3,] 3 6 9[1] "matrix" "array" 

3. Lists

  • Ordered collection of elements, which can be of different types.
  • Created using the list() function.
R
my_list <- list(1, "apple", TRUE, 3 + 4i)print(my_list)print(class(my_list))

Output:

[[1]][1] 1[[2]][1] "apple"[[3]][1] TRUE[[4]][1] 3+4i[1] "list"

4. Data Frames

  • Table-like structure where each column can contain different types of data.
  • Created using the data.frame() function.
R
df <- data.frame( numbers = c(1, 2, 3), fruits = c("apple", "banana", "cherry"), logicals = c(TRUE, FALSE, TRUE))print(df)print(class(df))

Output:

 numbers fruits logicals1 1 apple TRUE2 2 banana FALSE3 3 cherry TRUE[1] "data.frame"

Conclusion

R is a strong and flexible programming language used for statistics and data analysis. It is popular because it has many packages that help with tasks like data cleaning, visualization, and machine learning. R is especially good at creating detailed charts and graphs. As a free and open-source language, it is accessible to everyone and has a large community that shares code and offers support. While R can be slower with very large datasets and has a learning curve, ongoing improvements and integrations with other tools ensure it remains an essential tool for data scientists and analysts



T

tmishra2001

Improve

Next Article

Learn R Programming

Please Login to comment...

R Program Commands - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5557

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.