R Basics Examples

Begin with printing and simple input/output. These concise examples build comfort with R syntax and console interaction.

R Program to Make a Simple Calculator

Program (R)
Example & Expected Output

Uses switch to apply an operation.

[1] 13

Check if a Number is Odd or Even in R Programming

Program (R)
Example & Expected Output

Uses modulo to determine parity.

odd

R Program to Check if a Number is Positive, Negative or Zero

Program (R)
Example & Expected Output

Compares sign and prints result.

zero

R Program to Find the Factors of a Number

Program (R)
Example & Expected Output

Lists all positive divisors of a number.

[1] 1 2 4 7 14 28

R Program to Format Decimal Places

Program (R)
Example & Expected Output

Formats numbers with fixed decimals using sprintf.

[1] "3.14"

R Program to Concatenate Two Strings

Program (R)
Example & Expected Output

Concatenates strings using paste0.

[1] "Hello World"

Frequently Asked Questions

Install R (from CRAN). Save the code in a file like main.R, then run Rscript main.R from your terminal. Alternatively, use an IDE such as RStudio.

No. All examples rely on base R functions to ensure compatibility across systems. Where packages are useful, they’ll be explicitly mentioned.

Yes. Copy the code, tweak inputs, and observe outputs. Experimentation is the fastest way to build intuition and mastery.

Yes. R is cross-platform. The examples run on Windows, macOS, and Linux with a standard R installation.

Learn R the Practical Way

R is a powerful language for data analysis, visualization, and statistical computing. Practicing small, focused programs builds fluency with vectors, data frames, lists, functions, control flow, and string manipulation.

On this page, you’ll find clean, well-formatted examples that run across platforms. Each example includes a short description and expected output so you can quickly verify your understanding. Explore the topics via the sidebar and extend the code to experiment further.

Whether you’re preparing for coursework, handling data tasks at work, or learning R for research, these examples offer a practical path to mastery. Bookmark the page and revisit to keep sharpening your skills.