To start coding in R, install R and RStudio, then open RStudio and run your first command in the Console. That three-step setup is the shortest working path: R is the language engine, RStudio is the editor that makes it usable, and the Console is where you type code and see results immediately. From there, you write scripts, learn a handful of data types and functions, and practise loading and summarising a small dataset. This guide walks through that sequence in order, flags the mistakes beginners make most often, and explains what to do when something breaks.
What You'll Need
- A computer running Windows, macOS or Linux with at least a few GB of free disk space.
- An internet connection for the initial downloads and for installing packages.
- The R installer, downloaded from the official Comprehensive R Archive Network (CRAN) site.
- RStudio Desktop, the free version, downloaded from the official Posit site.
- A plain text editor is not required, but a spreadsheet program helps if you want to inspect CSV files before loading them.
Step-by-Step
- Install R first. Go to the CRAN website, choose the download link for your operating system, and run the installer with the default options. On Windows, the installer is an .exe file; on macOS, it is a .pkg file. Accept the default installation path unless you have a reason to change it. R installs the core language and a basic graphical interface, but you will rarely open that interface directly.
- Install RStudio Desktop. Go to the Posit website, find the RStudio Desktop download, and install the free version. RStudio will usually detect R automatically. If it does not, open RStudio and go to Tools > Global Options > General (on macOS, RStudio > Preferences > General) and set the R version manually. Menu labels can differ slightly between versions and operating systems.
- Open RStudio and find the four panes. The Console (usually bottom left) is where commands run. The Source editor (top left) is where you write and save scripts. The Environment pane (top right) lists the objects you have created. The Files/Plots/Packages/Help pane (bottom right) shows files, graphs, installed packages and documentation. If a pane is missing, use View > Panes to restore it.
- Run your first command. Click in the Console, type
2 + 2and press Enter. R prints[1] 4. The[1]is just a position marker, not part of the answer. Tryprint("Hello, R")next. If you see output, your installation works. - Create a script. Use File > New File > R Script. Type a few lines, such as
x <- 10andy <- 5andx + y. Place the cursor on a line and press Ctrl+Enter (Windows/Linux) or Cmd+Enter (macOS) to run it. Save the script with File > Save and give it a name ending in .R. Scripts let you rerun and edit code without retyping it. - Learn the assignment arrow. In R,
<-assigns a value to a name. Typingage <- 30stores 30 in an object called age. You can also use=in many places, but<-is the conventional choice and avoids confusion inside function calls. - Understand the basic data types. R has numeric values (1.5), integers (2L), characters or strings ("text"), logicals (TRUE and FALSE), and factors for categorical data. Check a type with
class()and a structure withstr(). These two functions will save you hours when something behaves unexpectedly. - Work with vectors. A vector is an ordered list of values of the same type. Create one with
c(1, 2, 3)orc("a", "b"). Do arithmetic on the whole vector at once:c(1, 2, 3) * 2returns 2, 4, 6. This vectorised behaviour is central to R and is why loops are often unnecessary. - Load a small dataset. R ships with built-in datasets. Run
data(mtcars)thenhead(mtcars)to see the first rows. Usesummary(mtcars)for quick statistics andstr(mtcars)for the column types. Practising on built-in data avoids file-path problems while you learn. - Read a CSV file. Put your file in a known folder and set the working directory with
setwd("path/to/folder"), or use an RStudio project to manage paths. Then runmydata <- read.csv("filename.csv"). Check the result withhead(mydata). On Windows, use forward slashes or double backslashes in paths. - Install and load a package. Packages add functions. Install one once with
install.packages("dplyr"), then load it in each session withlibrary(dplyr). Installation needs an internet connection; loading does not. If a package fails to install, check that your R version is current and that you have write access to the library folder. - Write a short analysis. A realistic beginner workflow: load data, inspect it with
str()andsummary(), clean obvious issues, compute a summary such asmean(mydata$column), and save a plot withplot()orhist(). Save the script so the whole analysis can be rerun from scratch. - Learn where help lives. Type
?function_namein the Console to open that function's help page. The Help pane also has a search box. Reading the Examples section at the bottom of a help page is often faster than searching the web.
Common Mistakes
- Installing RStudio before R. RStudio is an editor, not the language. Without R installed, it cannot run code.
- Forgetting quotation marks around file paths and package names.
read.csv(mydata.csv)fails;read.csv("mydata.csv")works. - Using the wrong slash direction in Windows paths. R treats a single backslash as an escape character. Use forward slashes or double backslashes.
- Confusing
<-with=inside function arguments. Assignment inside a function call usually means you meant to set a named argument. Use<-for storing values. - Not loading a package after installing it.
install.packages()only downloads;library()makes the functions available in the current session. - Ignoring warning messages. Warnings often signal a type mismatch or a calculation that produced NA. Read them before continuing.
- Retyping commands instead of using a script. Scripts are reproducible and easier to debug than a long Console history.
Tips
- Use RStudio Projects (File > New Project) to keep each analysis in its own folder with its own working directory. This prevents path confusion.
- Name objects descriptively, such as
sales_2024rather thanx1. Avoid spaces and special characters in names. - Comment your code with
#. A short note above each block explains why the step exists, not just what it does. - Run code line by line when learning. Running a whole script at once hides which step failed.
- Keep R and RStudio updated. Older versions can fail to install newer packages.
- When you get an error, copy the exact message into a search engine. R error messages are often specific enough to find a direct answer.
Troubleshooting
If RStudio opens but the Console shows no prompt, or commands do nothing, restart RStudio. If that fails, restart the computer. A hung session is usually cleared by Session > Restart R, which clears the Environment but keeps your saved scripts.
If a package will not install, check the exact spelling of the package name and confirm you are online. On corporate or school networks, a firewall may block CRAN; try a different network. If installation reports a permission error, R may be trying to write to a protected library folder. Installing to a user library or running RStudio with the appropriate permissions usually resolves it.
If read.csv() returns an error about the file not being found, print your working directory with getwd() and compare it with where the file actually sits. Use list.files() to see what R can see in that folder. Setting the working directory correctly, or using an RStudio Project, fixes most file errors.
If a calculation returns NA, check for missing values with is.na() and read the function's help page for its handling of missing data. Many functions accept an argument such as na.rm = TRUE to ignore missing values, but confirm the exact argument name in the help page before using it.
If plots do not appear, check the Plots pane in the bottom-right area. If it is hidden, restore it from View > Panes. For scripts run from the command line outside RStudio, plots may need an explicit device such as pdf() or png() to save output to a file.
Frequently Asked Questions
Do I need to know another programming language first?
No. R is a reasonable first language, especially for data work. Basic familiarity with spreadsheets helps but is not required.
Is R free?
Yes. R itself is free and open source, and RStudio Desktop has a free version. Some commercial add-on products are paid, but you do not need them to learn.
What is the difference between R and RStudio?
R is the language and the engine that runs your code. RStudio is an integrated editor that makes writing, running and organising R code easier. You need R installed for RStudio to work.
Why does my code work in the Console but not in a script?
The Console remembers objects you created earlier in the session; a fresh script run does not. Make sure every object a script uses is created or loaded earlier in that same script.
How do I update R?
Download and run the latest installer from CRAN. On Windows, you can also use the installr package, but the official installer is the simplest route. After updating R, you may need to reinstall packages.
Can I use R for machine learning?
Yes, through packages such as caret and tidymodels. Start with data manipulation and plotting first; those skills carry over directly.