R for Beginners

GuideUpdated October 27, 20268 min read

UndergraduateGraduateRecent graduate

If you study biology, psychology, public health, economics, sociology or almost any research-oriented subject, you will probably meet R. It was built by and for statisticians, and it shows: the language treats data tables, statistical models and graphics as first-class citizens. For students who need to analyze data, produce clear charts and write reproducible reports, R is a powerful and free tool. This guide introduces the essentials with small examples and a path to a first analysis.

R and its packages evolve continuously. This guide describes stable fundamentals. Check current documentation for installation steps and package details.

What R is good at

  • Statistical analysis: descriptive statistics, tests, regression and many expert methods.
  • Data visualization: flexible, high-quality graphics.
  • Data wrangling: cleaning and reshaping tables.
  • Reproducible research: combining code, results and narrative in one document.
  • Expert packages: a huge collection for fields such as genetics, ecology, social science and finance.
  • A strong academic community.

Less ideal uses include building general-purpose applications or low-level systems, where other languages may be more suitable.

R and other tools

  • Spreadsheets are great for small tasks and quick checks. R scales better and records every step.
  • Python is a general-purpose language with strong data libraries. Many analysts learn both.
  • SQL retrieves data from databases, and R can connect to databases.
  • Statistical packages with menus are easy to click through, but R's code makes analyzes repeatable.

Choose what your course and field use. Concepts transfer.

Set up

  1. Install R from the official source.
  2. Install a friendly editor. An integrated development environment designed for R makes scripts, console, plots and help easier to navigate.
  3. Create a project folder for each analysis, with subfolders for data, scripts and outputs.
  4. Use version control if you can.
  5. Keep your system updated and be careful installing packages from trusted repositories only.

Browser-based options exist if you cannot install software.

Basic syntax

R can act as a calculator.

2 + 3
sqrt(16)

Assign values to names with the assignment arrow.

x <- 10
name <- "Sam"
scores <- c(72, 65, 88, 91)

The c function combines values into a vector, R's basic data structure. Functions take arguments in brackets.

mean(scores)
summary(scores)

Comments start with a hash symbol.

Data structures

Vectors

Ordered collections of values of the same type. Most operations work on whole vectors at once.

scores * 2
scores[1]
scores[scores > 80]

Factors

Categorical variables with defined levels.

Lists

Collections that can hold different types.

Data frames

The workhorse of R: tables with rows and columns, where each column is a variable.

students <- data.frame(
  name = c("Sam", "Priya", "Alex"),
  course = c("Biology", "Chemistry", "Biology"),
  score = c(72, 88, 65)
)

You can access columns by name, filter rows and add new columns.

students$score
students[students$course == "Biology", ]
students$passed <- students$score >= 70

Import data

Real analysis starts with files.

  • Read data from comma-separated files and spreadsheets using functions or packages designed for importing.
  • Check the structure with functions that show dimensions, column types and the first rows.
  • Check for problems: wrong types, odd characters, missing values.
  • Use relative paths within your project so that your scripts work on other computers.
  • Protect personal data. Follow privacy rules, anonymize where possible and do not commit sensitive files to public repositories.

Clean and transform

Data cleaning often takes most of the time.

  • Handle missing values deliberately.
  • Fix data types such as dates and categories.
  • Standardize labels and spelling.
  • Remove duplicates.
  • Filter, select, rename and sort.
  • Create new variables.
  • Summarize by group.
  • Join tables by common keys.
  • Reshape data between wide and long formats.

Many R users adopt a consistent set of packages for data manipulation that provides readable functions for these tasks. The idea of tidy data is central: one variable per column, one observation per row, one table per kind of observational unit. Document each cleaning step in your script so that anyone can reproduce your results.

Summarize and explore

  • Descriptive statistics: means, medians, standard deviations, counts and proportions.
  • Cross-tabulations of categorical variables.
  • Look at distributions using histograms or box plots.
  • Compare groups.
  • Check relationships between variables.
  • Look for outliers and errors.

Exploration guides the choice of methods.

Visualize

R's graphics are a major strength.

  • Base graphics offer quick plots.
  • A widely used grammar-of-graphics package builds charts by combining layers: data, aesthetic mappings and geometric shapes.
library(ggplot2)

ggplot(students, aes(x = course, y = score)) +
  geom_boxplot()

Good visualization practice:

  • Choose chart types that fit the data: scatter plots for relationships, bar charts for counts, line charts for trends, histograms for distributions.
  • Label axes and titles.
  • Use color carefully and accessibly.
  • Avoid clutter.
  • Be honest with scales.
  • Save plots at suitable sizes for reports and slides.

Basic statistical analysis

R includes functions for common methods.

  • Comparing two groups: t-tests and nonparametric alternatives.
  • Comparing several groups: analysis of variance.
  • Associations between categorical variables: chi-squared tests.
  • Correlation.
  • Linear and logistic regression.
  • Confidence intervals.

Use methods that match your data and question, and check assumptions. Do not run tests blindly until one gives a significant result. Understand what a p-value does and does not tell you. Report effect sizes and uncertainty, not just significance. If you need help, consult your lecturer, a statistics support service or a statistician.

Reproducible reports

One of R's best features is combining code and writing.

  • Create documents that mix text, code and output. When you re-run the document, results update automatically.
  • Export to common formats for reports, slides or web pages.
  • Include code chunks, figures and tables.
  • Share the source with collaborators or supervisors.
  • Set a random seed when using random processes so that results can be reproduced.
  • Record the package versions used.

Reproducibility is increasingly expected in research and valued by employers.

Packages

Packages extend R.

  • Install once, load in each session.
  • Use well-known, maintained packages with good documentation.
  • Read the vignettes and examples.
  • Check licenses and citation requirements.
  • Be wary of unknown packages. Install from trusted sources. Our digital safety section describes general software safety.
  • Keep an environment record so that your project works later.

Good habits

  • Use scripts, not just the console, so that your work is saved.
  • Name variables clearly.
  • Comment on why you do things.
  • Use consistent style.
  • Keep raw data untouched.
  • Organize projects with folders.
  • Use version control.
  • Test small pieces before running big analyzes.
  • Read error messages carefully.
  • Back up your work.
  • Ask for help clearly, with a minimal example of your problem.

A first analysis project

  1. Choose a dataset from an open data source or your course, and read the documentation.
  2. Write your question.
  3. Import and inspect the data.
  4. Clean it and record changes.
  5. Explore with summaries and plots.
  6. Apply a suitable method to answer your question.
  7. Interpret results carefully.
  8. Create a reproducible report with clear figures and a conclusion.
  9. Share it in a repository with a README.

Examples: exam scores by study habits, air quality over time, survey results from a society, or a public health dataset.

Ethics and integrity

  • Respect consent and privacy.
  • Follow your university's ethics rules.
  • Report honestly, including negative or unclear results.
  • Don't p-hack or cherry-pick.
  • Cite data and software.
  • Be careful with AI tools. Check any generated code, follow course rules and disclose use where required.
  • Credit collaborators.

A short debugging routine

When a command fails, read the error message slowly, check the spelling of function and variable names and make sure the package is loaded. Inspect your data with structure and summary functions to see whether types and missing values are what you expect. Run the pipeline one step at a time. Search for the exact message with context, and when you ask for help, include a small reproducible example. These habits solve most problems and make you a better teammate.

It also helps to read reports written by others in your field, noting how they describe data, methods and limits. Imitating clear reporting is one of the quickest ways to improve your own.

Keep a list of the packages you rely on and why, with notes on what each does. When you return to a project months later, the list will save you time.

Common mistakes

  • Doing everything in the console and losing work.
  • Not checking data types.
  • Ignoring missing values.
  • Running tests without understanding.
  • Poor labeling on plots.
  • Hard-coding file paths.
  • Forgetting to record package versions and seeds.

A quick checklist

  • Do I have R and an editor installed, and a project folder?
  • Can I create vectors and data frames and import data?
  • Can I clean, summarize and plot data?
  • Do I understand the basics of the statistical methods I use?
  • Is my analysis documented and reproducible?

Final thoughts

R rewards patience with powerful tools for statistics and graphics. Learn the basics of vectors and data frames, practice cleaning and plotting and write scripts that record every step. Choose statistical methods thoughtfully, report honestly and produce reproducible documents. Those habits will help you across research, study and work.

Frequently asked questions

What is R used for?

Statistical analysis, data visualization and reporting, especially in research, health, social sciences and data science.

Should I learn R or Python?

Both are widely used. R is strong for statistics and visualization, Python is a general-purpose language with broad data tools. Choose according to your field and course.

Do I need to install anything?

You can install R and a friendly editor on your computer, or use browser-based environments.

Is R hard to learn?

The basics are approachable, especially with guided tutorials, though some conventions feel unusual to newcomers.

Can R make reports?

Yes. You can combine code, results and text in reproducible documents.