Fundamentals 10 min read

Master R Basics: Core Syntax, Workspace, and Data Types Explained

This guide introduces R's case‑sensitive syntax, workspace management, variable handling, input/output methods, commenting styles, and core data structures such as vectors, matrices, arrays, data frames, factors, and lists, providing essential commands and functions for each concept.

Model Perspective
Model Perspective
Model Perspective
Master R Basics: Core Syntax, Workspace, and Data Types Explained

R Language Basic Syntax

R is a case‑sensitive interpreted language with several data types, including vectors, matrices, data frames, and lists. Most functionality comes from built‑in functions or user‑defined functions, and objects created during a session reside in memory. Basic functions are available by default, while others require loading packages.

Workspace

The workspace stores all objects defined in the current R session. It can be saved to an image file and reloaded later. The working directory is the default location for reading and writing files. Use getwd() to view the current directory and setwd() to change it.

Variables

Valid variable names consist of letters, numbers, periods, and underscores ( _ ). Assignment can be done with the left arrow, equal sign, or right arrow, but using = is discouraged. List defined variables with ls() and remove them with rm() .

Input and Output

R starts an interactive session that reads from the keyboard and prints to the screen, but scripts can also be executed. Redirect output to a file with sink("filename") ; use append=TRUE to add to an existing file or split=TRUE to send output to both screen and file. Graphical output can be saved with pdf("filename") or png("filename") , and finalized with dev.off() .

Comments in R

Comments start with # . Multi‑line comments can be simulated with if(FALSE){ ... } .

R Language Data Types

R provides various object types for storing data, such as vectors, matrices, arrays, data frames, factors, and lists, each differing in storage mode, memory usage, creation method, and structural complexity.

Vectors

Vectors store numeric, character, or logical data in a one‑dimensional array and are created with c() . All elements must be of the same type. Numeric vectors can be summarized with sum() , mean() , var() , sd() , max() , etc. Character vectors use functions like toupper() , tolower() , nchar() , strsplit() , and gsub() . Logical vectors are processed with which() , all() , and any() .

Matrices

Matrices are two‑dimensional arrays of a single data type, created with matrix() . Parameters include data , nrow , ncol , byrow , and dimnames . Elements are accessed with X[i,] , X[,j] , or X[i,j] . Transpose with t() and multiply matrices with the %*% operator.

Arrays

Arrays are similar to matrices but can have more than two dimensions, created with array() . The apply() function performs operations across dimensions, using the syntax apply(X, MARGIN, FUN) where MARGIN specifies the dimension.

Data Frames

Data frames are two‑dimensional tables where each column has a unique name and consistent type. Create them with data.frame() . Summarize with summary() , select rows/columns by index or $ , and combine data frames with cbind() (columns) or rbind() (rows).

Factors

Factors store categorical data, either nominal or ordered. Create them with factor() . The gl() function generates factor levels.

Lists

Lists are ordered collections that can contain heterogeneous objects, including other lists. Create them with list() , name elements with names() , and convert to vectors with unlist() .

programmingstatisticsData TypesfundamentalsR
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.