Unit 5 R
Unit 5 R
It has many features which are useful It can be used to develop GUI
Objective for statistical analysis and applications and web applications as
representation. well as with embedded systems
Integrated development Various popular R IDEs are Rstudio, Various popular Python IDEs are
environment RKward, R commander, etc. Spyder, Eclipse+Pydev, Atom, etc.
It supports Tidyverse
and it became easy to Use can you NumPy,
Data modeling import, manipulate, SciPy, scikit-learn,
visualize, and report on TansorFlow
data
Capability R Python
Basic Statistics Built-in functions (mean, median, etc.) NumPy (mean, median, etc.)
Statsmodels (OLS)
Linear Regression lm() function and Formulas
Ordinary Least Squares (OLS) Method
Time Series Analysis Time Series packages (forecast) Statsmodels (Time Series)
ANOVA and t-tests Built-in functions (aov, t.test) SciPy (ANOVA, t-tests)
Primary users are Scholar and R&D Primary users are Programmers and developers
Support packages like tidyverse, ggplot2, caret, Support packages like pandas, scipy, scikit-learn,
zoo TensorFlow, caret
single-line comments
# this code prints Hello World
print("Hello World")
multi-line comments
# this is a print statement
# it prints Hello World
print("Hello World")
R Variables and Constants
2. Integer Variables 3. Floating Point Variables
Types of R A = 14L x = 13.4
Variables print(A) print(x)
print(class(A)) print(class(x))
1. Boolean [1] 14 Output
[1] "integer" [1] 13.4
Variables [1] "numeric"
4. Character Variables
a = TRUE alphabet = "a"
print(alphabet)
print(a) print(class(alphabet))
Output
print(class(a)) [1] "a“
[1] "character"
[1] TRUE
5. String Variables
[1] "logical“ message = "Welcome to R“
print(message)
print(class(message))
Output
[1] "Welcome to R"
[1] "character"
R Constants
Constants are those entities whose values aren't meant to be changed anywhere
throughout the code. In R, we can declare constants using the <- symbol.
x <- "Welcome to R"
print(x)
Output
[1] "Welcome to R“
• Types of R Constants
• In R, we have the following types of constants.
• The five types of R constants - numeric, integer, complex, logical, string.
• In addition to these, there are 4 specific types of R constants
- Null, NA, Inf, NaN.
1. Integer Constants Numeric Constants
x <- 15L print(typeof(x)) print(class(x)) 3. Logical Constants
z<- 3e-3
Output x <- TRUE
print(z) # 0.003
[1] "integer" y <- FALSE
[1] "integer“ print(class(z)) # "numeric”
print(x)
y <- 3.4
print(y)
# hexadecimal value print(y) # 3.4
Output
x <- 0x15L print(class(z)) # "numeric"
[1] TRUE
print(x) # exponential value Output
x <- 1e5L [1] FALSE
[1] 0.003
print(x) 5. Complex Constants
[1] "numeric"
Output
[1] 3.4 y <- 3.2e-1i
[1] 21
[1] 100000 [1] "numeric" print(y)
print(typeof(y))
4. String Constants Output
message <- "Welcome to R" [1] 0+0.32i
print(message) [1] "complex"
Output
[1] "Welcome to R"
x <- NULL print(x) NULL
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S“"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“ "t" "u" "v" "w" "x" "y" "z"
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
[1] 3.141593
Different
bool1 <- TRUE
Types of Data Types
• logical print(bool1) # floating point values
print(class(bool1)) weight <- 63.5
• numeric [1] TRUE print(weight)
[1] "logical" print(class(weight)) # real numbers
• integer height <- 182
print(height)
• complex var1 <- 186L
print(class(height))
• character print(class(var1))
Output
[1] 63.5
[1] "numeric"
• raw [1] "integer" [1] 182
[1] "numeric"
# 2i represents imaginary part
# create a string variable
complex_value <- 3 + 2i
fruit <- "Apple"
# print class of complex_value
print(class(fruit))
print(class(complex_value))
# create a character variable
[1] "complex"
my_char <- 'A'
print(class(my_char))
[1] "character" [1] "character"
A raw data type specifies values as raw bytes. You can use the following methods to convert
character data types to a raw data type and vice-versa:
charToRaw() - converts character data to raw data
rawToChar() - converts raw data to character data
numb1 = 8
numb2 = 4
sum = numb1 + numb2
print("The sum is",
sum)
R Print Output
# print values [1] "R is fun"
print("R is fun") [1] "Welcome to R"
# print variables
x <- "Welcome to R"
print(x)