What are the basic R commands?

R commands are the basis for data analysis and statistical modeling in the R environment. They provide the tools and flexibility to read data, identify patterns and make informed decisions.

What are R commands?

R commands are used in R programming to perform specific tasks or initiate actions in the R environment. These commands make it possible to analyze data, perform statistical calculations, or create visualizations. R commands can be entered and processed in the R command line or in R scripts. It’s important to distinguish 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 associated with different R data types .

Tip

With Webspace from IONOS, you’ll benefit from a minimum of 50 GB of free space and high-performance, high-availability servers that ensure your website is always online and loads quickly. Plus, you’ll get a free domain and an SSL wildcard certificate for site safety.

An overview of R commands

The following R commands list provides an overview of different application areas in R programming. Depending on your specific needs and projects, you can pick and match the commands that suit you.

Data manipulation and processing

  • read.csv(): Read data from a CSV file
  • data.frame(): Create a data framework
  • 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

Data visualization

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

Statistical analysis

  • summary(): Get a summary of data, including statistical metrics
  • lm(): Perform linear regressions
  • t.test(): Perform T-tests for hypothesis testing
  • cor(): Calculate correlation coefficients between variables
  • anova(): Perform analysis of variance (ANOVA)
  • chi-sq.test(): Perform chi-square tests

Data processing

  • ifelse(): Perform condition evaluations and conditional expressions
  • 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

Statistical graphs and charts

  • qqnorm(), qqline(): Create quantile-quantile diagrams
  • plot(), acf(): Display autocorrelation diagrams
  • density(): Display density functions and histograms
  • 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 customize 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 relationship between the variables X and Y from the data data frame. The argument main defines the diagram title.

Performing linear regression

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

In this example, we’ll perform a linear regression to model the relationship between the variables X and Y from the data data frame. The lm() command is used to calculate a linear regression in R. The result of the regression 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 manipulation. The dplyr package offers powerful data filtering capabilities. 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 distribution with a normal distribution.

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

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.