§2024-12-03
R is often used for statistical computing and graphical presentation to analyze and visualize data. R is a programming language, originally ‘written’ in 1992 by Ross Ihaka and Robert Gentleman (yes, “R” stems from the first letter of their first names), inspired by another programming language called S.
- install in ubuntu
sudo apt install r-base r-base-dev -y
Summary:
r-base: Stable version of R suitable for most users.
r-development: Latest development version of R, for testing and development purposes.
alexlai@n2Bookworm:~/Downloads$ R
R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: aarch64-unknown-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
¶Rules to Declare R Variables
As per our requirements, we can use any name for our variables. However, there are certain rules that need to be followed while creating a variable:
- A variable name in R can be created using letters, digits, periods, and underscores.
- You can start a variable name with a letter or a period, but not with digits.
- If a variable name starts with a dot, you can't follow it with digits.
- R is case sensitive. This means that age and Age are treated as different variables.
- We have some reserved words that cannot be used as variable names.
.1 <- 1, a variable name starts with a dot, you can't follow it with digits
¶Basic data types in R can be divided into the following types:
- numeric - (10.5, 55, 787)
- integer - (1L, 55L, 100L, where the letter "L" declares this as an integer)
- complex - (9 + 3i, where "i" is the imaginary part)
- character (a.k.a. string) - ("k", "R is exciting", "FALSE", "11.5")
- logical (a.k.a. boolean) - (TRUE or FALSE)
## Online R compiler to run R program online
## Print "Try programiz.pro" message
# 1. Boolean Variables
a = TRUE
print(a)
print(class(a))
# 2. Integer Variables
A = 14L # L is integer
print(A)
print(class(A))
#3. Floating Point Variables
# It stores numeric data with decimal values. For example,
x = 13.4
print(x)
print(class(x))
# 4. Character Variables
# It stores a single character data. For example,
alphabet = "a"
print(alphabet)
print(class(alphabet))
# 5. String Variables
# It stores data that is composed of more than one character. We use double quotes to represent string data. For example,
message <- "Welcome to Programiz!"
print(message)
print(class(message))
# 6. Changing Value of Variables
# Depending on the conditions or information passed into the program, you can change the value of a variable. For example,
message = "Hello World!"
print(message)
# changing value of a variable
message <- "Welcome to Programiz!"
print(message)
¶Constant
In R, you can create constants by assigning values to variables in a way that prevents their modification later on. However, R does not have built-in support for truly constant variables like in some other programming languages (e.g., const in C or Python). But, you can simulate constants by following certain practices.
Here are a few common ways to define constants in R:
- Using assign() with lockBinding()
# Define a constant
MY_CONSTANT <- 3.14159
# Lock the constant to prevent modification
lockBinding("MY_CONSTANT", globalenv())
# Try modifying the constant (this will produce an error)
MY_CONSTANT <- 2
# Error: cannot change value of locked binding for 'MY_CONSTANT'
Conclusion
Although R doesn't provide a true constant feature like some other languages, you can use one of the methods above to simulate constants and discourage modification of certain values. The most reliable way is the lockBinding() method.
¶Built-In R Constants
R programming provides some predefined constants that can be directly used in our program. For example,
# print list of uppercase letters
print(LETTERS)
# print list of lowercase letters
print(letters)
# print 3 letters abbreviation of English months
print(month.abb)
# print numerical value of constant pi
print(pi)
- output
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
[1] 3.141593
¶Different Types of Data Types
In R, there are 6 basic data types:
- logical
- numeric
- integer
- complex
- character
- raw
# logical
bool1 <- TRUE
print(bool1)
print(class(bool1))
bool2 <- FALSE
print(bool2)
print(class(bool2))
# Note: You can also define logical variables with a single letter - T for TRUE or F for FALSE. For example,
is_weekend <- F
print(class(is_weekend)) # "logical"
# In R, the numeric data type represents all real numbers with or without decimal values. For example,
# floating point values
weight <- 63.5
print(weight)
print(class(weight))
# real numbers
height <- 182
print(height)
print(class(height))
# 3. Integer Data Type
# The integer data type specifies real values without decimal points. We use the suffix L to specify integer data. For example,
integer_variable <- 186L
print(class(integer_variable))
# 4. Complex Data Type
# The complex data type is used to specify purely imaginary values in R. We use the suffix i to specify the imaginary part.
# 2i represents imaginary part
complex_value <- 3 + 2i
print(complex_value)
# print class of complex_value
print(class(complex_value))
# 5. Character Data Type
# '' for character variables
# "" for string variables
# create a string variable
fruit <- "Apple"
print(fruit)
print(class(fruit))
# create a character variable
my_char <- 'A'
print(my_char)
print(class(my_char))
# 6. Raw Data Type
# A raw data type specifies values as raw bytes.
# convert character to raw
raw_variable <- charToRaw("Welcome to Programiz")
print(raw_variable)
print(class(raw_variable))
# convert raw to character
char_variable <- rawToChar(raw_variable)
print(char_variable)
print(class(char_variable))
¶R print() Function
# R print() Function
# In R, we use the print() function to print values and variables. For example,
# print values
print("R is fun")
# print variables
x <- "Welcome to Programiz"
print(x)
¶paste() Function in R
You can also print a string and variable together using the print() function. For this, you have to use the paste() function inside print(). For example,
company <- "Programiz"
# print string and variable together
# By default, you can see there is a space between string Welcome to and the value Programiz
print(paste("Welcome to", company))
company <- "Programiz"
# using paste0() instead of paste()
# If you don't want any default separator between the string and variable, you can use another variant of paste() called paste0().
print(paste0("Welcome to", company))
¶R sprintf() Function
The sprintf() function of C Programming can also be used in R. It is used to print formatted strings. For example,
myString <- "Welcome to Programiz"
# print formatted string
sprintf("String: %s", myString)
Specifier Value Type
%c Character %d or %i Signed Decimal Integer %e or %E Scientific notation %f Decimal Floating Point %u Unsigned Decimal Integer %p Pointer address
# sprintf() with integer variable
myInteger <- 123
sprintf("Integer Value: %d", myInteger)
# sprintf() with float variable
myFloat <- 12.34
sprintf("Float Value: %f", myFloat)
¶R cat() Function
R programming also provides the cat() function to print variables. However, unlike print(), the cat() function is only used with basic types like logical, integer, character, etc.
# print using Cat
cat("R Tutorials\n")
# print a variable using Cat
message <- "Programiz"
cat("Welcome to ", message)
¶Print Variables in R Terminal You can also print variables inside the R terminal by simply typing the variable name. For example,
# inside R terminal
x = "Welcome to Programiz!"
# print value of x in console
x
Output: [1] "Welcome to Programiz"