R language basics
R language basics
Basic Fundamentals
R is an environment for data manipulation, statistical computing, graphic display
and data analysis. Effective data handling and storage of output is possible.
Simple as well as complicated calcu possible.
History and Applications: R has its roots in the S language created at Bell
Laboratories. It's used in various fields such as academia, healthcare, and
finance for data analysis and visualization.
Key Features
Comprehensive collection of statistical tools.
Advantages of R
Open-source and free to use.
Wide variety of libraries and packages for data analysis and visualization.
UNIT 1 Notes 1
Installation and Use of Software
Downloading R: You can download R from the Comprehensive R Archive
Network (CRAN) at https://cran.r-project.org/. Follow the instructions on the
website to install R for your operating system (Windows, macOS, Linux).
UNIT 1 Notes 2
UNIT 1 Notes 3
UNIT 1 Notes 4
Creating a vector
Vector entries can be calculations or previously stored items including vectors
themselves
UNIT 1 Notes 5
myvec←c(1,3,4,2,4)
myvec
foo<-32.1
newvec<-(1,2,3,4,foo)
myvec3<-(myvec,newvec)
Slicing
3:27
foo=5.3
bar=foo:(-47+1.5)
bar
seq(from=3,to=27,by=3)
seq(from=3,to=27,length.out=40)
length.out means the gaps it will form…which means here 39 gaps will be formed
UNIT 1 Notes 6
rep(x=1,times=4)
x=c((3,4,2),times=3)
#342342342
rep(x,each=2)
#3 3 4 4 2 2
rep(x,each=2,times=3)
sort(x=c(2,4,5,19,33),decreasing=FALSE)
#for ascending order
foo=seq(from=4.3,to=5.5,length.out=8)
bar=sort(x=foo,decreasing=TRUE)
Vector length
length(x=c(1,2,3,4))
myvec<-c(5,-2,3,4,4,4,-8)
length(x=myvec)
#7
myvec[1]
#5
foo<-myvec[2]
foo
#-2
myvec[length(myvec)]
#-8
UNIT 1 Notes 7
Command line Vs Script
Execution of commands in R is not menu driven.
when writing multi line program it is useful to use a text editor rather than
executing everythn directly at the command line.
1.One may use R’s own built in editor. it is accessible from R gui menu bar.
2. Use R studio software.
Introduction to R studio
it is an interface b/w R and us.
UNIT 1 Notes 8
x=3
y=4
rm(x,y)
UNIT 1 Notes 9
3. Data Editing
Use the data.frame or matrix structure to store tabular data.
Example:
4. Using R as a Calculator
Basic arithmetic:
5 + 3 # Addition
5 - 3 # Subtraction
5 * 3 # Multiplication
5 / 3 # Division
5^3 # Exponentiation
UNIT 1 Notes 10
Defining Functions
Creating reusable blocks of code (functions)
Assignments
Use <- or = to assign values: assigning values to variables for computation
and data manipulation.
x <- 10
y = 20
z <- x + y
print(z)
6. R Packages
Collections of pre-built functions and datasets in R that extend its functionality for
specialized tasks
To install a package:
install.packages("ggplot2")
Load a package:
library(ggplot2)
UNIT 1 Notes 11
installed.packages()
x <- 5 + 3 # Expression
Objects
Everything in R is an object (e.g., vectors, matrices, lists).
Symbols
Names assigned to objects.
Functions
Built-in or user-defined reusable operations that perform specific tasks.
8. Special Values
UNIT 1 Notes 12
Special Constants
operations.
NA : Missing value.
Examples
UNIT 1 Notes 13