R commands are the basis for data analysis and stat­ist­ic­al modeling in the R en­vir­on­ment. They provide the tools and flex­ib­il­ity to read data, identify patterns and make informed decisions.

What are R commands?

R commands are used in R pro­gram­ming to perform specific tasks or initiate actions in the R en­vir­on­ment. These commands make it possible to analyse data, perform stat­ist­ic­al cal­cu­la­tions, or create visu­al­isa­tions. R commands can be entered and processed in the R command line or in R scripts. It’s important to dis­tin­guish R commands from functions in R.

R functions are blocks of code defined and named in R that perform specific tasks. These can include the use of R operators and R data to accept arguments or output return values. This means that functions can store, process and return data as­so­ci­ated with different R data types .

Tip

With Webspace from IONOS, you’ll benefit from a minimum of 50 GB of free space and high-per­form­ance, high-avail­ab­il­ity servers that ensure your website is always online and loads quickly. Plus, you’ll get a free domain and an SSL wildcard cer­ti­fic­ate for site safety.

An overview of R commands

The following R commands list provides an overview of different ap­plic­a­tion areas in R pro­gram­ming. Depending on your specific needs and projects, you can pick and match the commands that suit you.

Data ma­nip­u­la­tion and pro­cessing

  • read.csv(): Read data from a CSV file
  • data.frame(): Create a data framework
  • subset(): Filter data based on specific con­di­tions
  • 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

Data visu­al­isa­tion

  • plot(): Create scatter plots and other basic plot types
  • hist(): Create his­to­grams
  • barplot(): Create bar charts
  • boxplot(): Create box plots
  • ggplot2::ggplot(): Create more soph­ist­ic­ated and cus­tom­is­able visu­al­isa­tions with the ggplot2 package

Stat­ist­ic­al analysis

  • summary(): Get a summary of data, including stat­ist­ic­al metrics
  • lm(): Perform linear re­gres­sions
  • t.test(): Perform T-tests for hy­po­thes­is testing
  • cor(): Calculate cor­rel­a­tion coef­fi­cients between variables
  • anova(): Perform analysis of variance (ANOVA)
  • chi-sq.test(): Perform chi-square tests

Data pro­cessing

  • ifelse(): Perform condition eval­u­ations and con­di­tion­al ex­pres­sions
  • apply(): Apply a function to matrices or data frames
  • dplyr::filter(): Filter data in data frames with the dplyr package
  • dplyr::mutate(): Create new variables in data frames with the dplyr package
  • lapply(), sapply(), mapply(): Apply functions to lists or vectors

Data import and export

  • readRDS(), saveRDS(): Read and save R data objects
  • write.csv(), read.table(): Export and import data in various formats

Stat­ist­ic­al graphs and charts

  • qqnorm(), qqline(): Create quantile-quantile diagrams
  • plot(), acf(): Display auto­cor­rel­a­tion diagrams
  • density(): Display density functions and his­to­grams
  • heatmap(): Create heat maps

R command examples

The following code examples show you how to use basic R commands for different purposes. Depending on your data and analysis needs, you can customise and extend these commands.

Reading data from a CSV file

data <- read.csv("data.csv")
R

Read.csv() is a command for reading data from a CSV file in R. In our example, the imported data is stored in the variable data. This command is useful for importing external data into R and making it available for analysis.

Creating a scatter plot

plot(data$X, data$Y, main="Scatter plot")
R

Plot() is one of the R commands for creating charts and graphs in R. Here, a scatter plot is drawn showing the re­la­tion­ship between the variables X and Y from the data data frame. The argument main defines the diagram title.

Per­form­ing linear re­gres­sion

regression_model <- lm(Y ~ X, data=data)
R

In this example, we’ll perform a linear re­gres­sion to model the re­la­tion­ship between the variables X and Y from the data data frame. The lm() command is used to calculate a linear re­gres­sion in R. The result of the re­gres­sion is stored in the variable regression_model and can be used for further analysis.

Filtering data with the dplyr package

filtered_data <- dplyr::filter(data, column > 10)
R

The command dplyr::filter() is derived from the dplyr package and used for data ma­nip­u­la­tion. The dplyr package offers powerful data filtering cap­ab­il­it­ies. We get the variable filtered_data by selecting rows from the data frame data where the value in the column is greater than 10.

Creating quantile-quantile diagrams

qqnorm(data$Variable)
qqline(data$Variable)
R

You can use qqnorm() to plot a quantile-quantile diagram in R. In this example, a quantile-quantile diagram for the variable variable is drawn from data. qqline() adds a reference line to compare the dis­tri­bu­tion with a normal dis­tri­bu­tion.

If you are just getting started with R, we recommend checking out our tutorial on R pro­gram­ming. Here, you’ll find useful tips and basic in­form­a­tion to get started with the language. For more tips and learning the basics of pro­gram­ming, our Digital Guide article on learning how to code has got you covered.

Go to Main Menu