Introduction to R Coding: Basics & First Steps

How-to basics · Updated November 22, 2026

R is a free, open-source programming language built for statistical computing, data analysis, and graphical visualization. It is one of the most widely used tools among statisticians, data scientists, and researchers because it offers a vast ecosystem of packages for almost any quantitative task.

What Is R and Where Did It Come From?

R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, in the early 1990s. It is an implementation of the S programming language, which was developed at Bell Laboratories in the 1970s. The name “R” is partly a nod to its creators’ first names and partly a play on the letter S. Today, R is maintained by the R Core Team and the R Foundation for Statistical Computing. It runs on Windows, macOS, and Linux, and you can download it for free from the Comprehensive R Archive Network (CRAN).

Unlike general-purpose languages like Python or Java, R was designed specifically for data analysis. Its syntax is concise for statistical operations, and it includes built-in functions for linear modeling, hypothesis testing, time series analysis, and more. This specialization is why many academic fields—especially biology, economics, and social sciences—adopt R for research.

How R Coding Works: Key Concepts

At its core, R works by storing data in objects and then applying functions to those objects. The most common object types are:

  • Vectors – a one-dimensional sequence of values (e.g., c(1, 2, 3))
  • Data frames – a table of rows and columns, similar to a spreadsheet
  • Lists – a collection of objects that can be of different types
  • Matrices – a two-dimensional array of numbers

You assign values with the arrow operator <- (or the equals sign =). For example, to store the numbers 5, 10, and 15 in a vector called my_numbers, you write:

my_numbers <- c(5, 10, 15)

Then you can apply functions like mean(my_numbers) to get the average (which returns 10). R also supports loops (for, while) and conditional statements (if, else), but most R users prefer to use vectorized operations—applying a function to an entire vector at once—because they are faster and more readable.

Setting Up Your R Environment

To start coding in R, you need two things: the base R software and an editor or interface. The most common setup is:

  1. Download and install R from CRAN.
  2. Install RStudio (now called Posit) – a free integrated development environment (IDE) that makes writing and running R code much easier.
  3. Launch RStudio and create a new R script (File > New File > R Script).

In RStudio, you write your code in the script pane, then run it by clicking “Run” or pressing Ctrl+Enter (Cmd+Enter on Mac). The results appear in the console, and any plots show in the lower-right panel. RStudio also offers a file browser, a history of commands, and a workspace viewer—all of which help beginners stay organized.

If you prefer not to install anything, you can use RStudio Cloud (now Posit Cloud) in a web browser, but for serious work, a local installation is more responsive and reliable.

Common R Packages and How to Install Them

Base R includes many functions, but its real power comes from packages—collections of functions and datasets contributed by the community. You install a package once using install.packages(), then load it with library() in each session. For example:

install.packages("ggplot2")
library(ggplot2)

Some of the most widely used packages for beginners include:

  • dplyr – for data manipulation (filtering, sorting, summarizing)
  • ggplot2 – for creating elegant and customizable plots
  • tidyr – for cleaning and reshaping data
  • readr – for reading CSV and other text files quickly
  • stringr – for working with text strings

These packages are part of the “tidyverse,” a collection of packages designed to work together seamlessly. Installing the whole tidyverse with install.packages("tidyverse") is a good starting point because it gives you all of the above at once.

Common Problems Beginners Face (and How to Fix Them)

When you start writing R code, you will likely run into a few typical errors:

  • “object not found” – you used a variable name that hasn’t been defined. Check spelling and make sure you assigned it earlier.
  • “could not find function” – you forgot to load the package that contains that function. Use library(package_name).
  • Unmatched parentheses or brackets – R will show a + prompt in the console, meaning it expects more input. Press Esc to cancel and fix the line.
  • Factor vs. character – when reading data, R may treat text columns as factors (categorical). Use stringsAsFactors = FALSE when reading, or convert with as.character().

A useful habit is to run your script line by line rather than all at once, so you can isolate where the error occurs. Also, use ?function_name to open the help page for any function—this is often faster than searching the web.

R vs. Python: Which Should You Choose?

R and Python are the two most popular languages for data work, and the choice often depends on your field and goals.

  • R excels at statistical analysis, data visualization, and reproducible research. It has a steep learning curve for general programming but is unmatched for specialized statistical tests and publication-quality plots.
  • Python is a general-purpose language that is easier to pick up if you also want to build web apps, automate tasks, or work with machine learning at scale. Libraries like pandas and matplotlib cover many of the same needs.

In practice, many data scientists learn both. If your work is primarily statistical (e.g., biostatistics, econometrics), R is a strong first choice. If you are heading into software engineering or deep learning, Python may be more practical. For beginners, start with one and switch later if needed—the underlying concepts (data types, functions, loops) transfer between them.

How to Learn R Effectively

Learning to code in R is like learning a new language: you need regular practice and real problems to solve. Here are some proven strategies:

  • Start with an interactive tutorial, such as “R for Data Science” by Hadley Wickham (free online) or the swirl package, which teaches R inside R itself.
  • Work with a dataset that interests you—sports stats, weather data, or something from your own field—and try to answer questions with R.
  • Join a community like Stack Overflow or the RStudio Community. Searching for error messages there will often lead you to the answer.
  • Replicate code from reputable blogs or textbooks, but always type it out yourself to build muscle memory.

Also, get comfortable reading documentation. R’s help files are dense but precise; learning to parse them will save you hours.

Frequently Asked Questions

Is R hard to learn for a complete beginner?

R has a reputation for being tricky because its syntax is different from general-purpose languages, and the error messages can be cryptic. However, with the tidyverse packages, many common operations are now quite intuitive. If you have no programming background, expect to spend a few weeks getting comfortable with the basics, but you can start making useful plots and summaries within your first session.

Do I need to know statistics to use R?

No, but it helps. R is a tool, and you can use it for simple tasks like sorting data or making charts without knowing advanced statistics. However, if you plan to run t-tests, regressions, or ANOVA, you should understand the underlying statistical concepts to interpret the output correctly. Many beginners learn statistics and R together.

Can I use R for machine learning?

Yes. R has packages like caret, tidymodels, and randomForest for building predictive models. It is less common for production-scale deep learning than Python, but for classical machine learning (regression, classification, clustering), R is fully capable.

Is R free to use commercially?

Yes, R is released under the GNU General Public License, which means it is free to use, modify, and distribute, even for commercial purposes. You can also sell products or services built with R without paying any licensing fees. However, some packages may have their own licenses, so check the specific package documentation if you are redistributing code.

How do I import an Excel file into R?

The easiest way is to use the readxl package. First install it with install.packages("readxl"), then load it and use read_excel("path/to/file.xlsx"). The function automatically detects the sheet and returns a data frame. For CSV files, use read.csv() or read_csv() from the readr package.

What is the difference between R and RStudio?

R is the programming language and the underlying engine that executes your code. RStudio is a separate, free application that provides a user-friendly interface for writing, running, and debugging R code. You can use R without RStudio, but RStudio makes the experience much more pleasant, especially for beginners.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.