LNCT UNIVERSITY, BHOPAL
Programme: BCA (AIDA) Semester: III
Subject: R Programming for Data Science and Data Analysis (BAI-305)
Type: SEC Credits: 3 (3-1-0)
UNIT I – Getting Started with R and R Workspace
Introduction to R: R is an open-source programming language used for statistical computing and data
analysis. It supports visualization, modeling, and data manipulation. Developed by Ross Ihaka and Robert
Gentleman. Features of R: Free, open-source, platform-independent, large community, and supports
various statistical techniques. Installing R & RStudio: R is installed from CRAN (Comprehensive R Archive
Network). RStudio is an integrated environment with panels: Console, Script Editor, Environment/History,
and Plots/Files/Packages panes. R Workspace: The workspace contains all user-defined objects (variables,
functions, data frames, etc.). Commands: ls() to list, rm() to remove, [Link]() to save workspace,
load() to load. Working Directory: It is the folder where R reads and writes files.
Example: getwd() – get current directory, setwd('C:/data') – set directory. Packages and Libraries:
Packages are collections of functions and datasets. Install using [Link]('ggplot2') and load with
library(ggplot2). Inspecting Environment: str() displays structure, class() shows data type, rm() removes
variables.
UNIT II – Basic Objects and Basic Expressions
Data Types in R: Numeric, Character, Logical, Complex, and Raw. Example: x <- 10, name <- 'R', flag <-
TRUE. Vectors: Ordered collection of same-type elements. v <- c(1,2,3). Access by index: v[2]. Matrix:
Two-dimensional array of same-type elements. matrix(1:9, nrow=3, ncol=3). List: Collection of different
types of elements. list(name='R', age=20, marks=c(90,95)). Data Frame: Table-like structure with rows
and columns. [Link](Name=c('A','B'), Age=c(20,21)). Factors: Used for categorical variables.
factor(c('Yes','No','Yes')). Operators: Arithmetic (+, -, *, /), Relational (==, !=, >, <), Logical (&, |, !),
Assignment (<-, =). Conditional Statements: if(x>0){print('Positive')} else {print('Negative')} Loops: for(i in
1:5){print(i)}; while(i<5){i=i+1} Functions: Defined using function() keyword. sumFun <- function(a,b){
return(a+b) }
UNIT III – Working with Basic Objects and Strings – I
Object Functions: class(), dim(), length(), str(), summary(). Data Dimensions: dim(df) returns rows and
columns count. Reshaping Data Structures: Functions like rbind(), cbind(), t() (transpose) modify
structure. Dealing with Missing Values: [Link]() checks, [Link]() removes missing entries. Logical
Operations: &, |, ! used for combining logical conditions. Math Functions: sin(), cos(), log(), exp(), sqrt(),
round(). Trigonometric Functions: sin(pi/2), cos(0). Statistical Functions: mean(), median(), var(),
sd(), range(), quantile(). Calculus: integrate(function(x) x^2, 0, 1) performs integration; differentiation via
symbolic packages.
UNIT IV – Working with Basic Objects and Strings – II
Sampling and Random Numbers: sample(1:10, 5) random sample, rnorm(5) normal distribution values.
Covariance and Correlation: cov(x, y) measures joint variability, cor(x, y) measures linear relationship.
String Functions: paste('Hello','World') → 'Hello World'
nchar('R') → 1
toupper('data') → 'DATA' Text Transformation: substr(), strsplit(), gsub(), grep() for string pattern search.
Date and Time: [Link](), [Link](), format([Link](), '%d-%m-%Y'). Reading and Writing Data:
[Link]('[Link]')
[Link](df, '[Link]') Data Cleaning: Remove missing values, convert formats, rename columns, and
validate consistency. Statistical Analysis: Functions: mean(), median(), sd(), cor(), summary() for
exploring datasets.
End of Notes – R Programming for Data Science and Data Analysis (BAI-305)