You are on page 1of 12

R1

Name Explanation
Class Returns class attributes of an object
as. Character Character that takes single parameter value that
represent numeric objects that needs to be
converted it returns a string data type.
getwd() Used to retrieve information about the current
working directory.
you can ensure that your R code interacts with
the correct files and directories.
Setwd() Managing your working directory is crucial for
loading data files, saving plots, and organizing
files.
Ensure that your R code interacts with the
correct files and directories.

Str() Displays the internal function of data set/object


such as array, list, data frame, vector, matrix.
Data frame Its 2D structure
Has rows and columns each column can be a
different vector each vector can have different
data type.
Each column should have data type and all
vectors should be same length.
Function Set of statements organized together to perform
a certain task.
Returns control to interceptor to result variable.
SYNTAX
F= function(arguments){
Statements
}

Components
Name- actual name stored in R environment
Arguments – (place holder) user passes value to
arguments
Body- contains a collection of statement tat
define a purpose of function
Written value – last expression in function body
to be evaluated.
FUNCTIONS
Self contained modules that accomplish a
specific task
Can be called over and over again which is
useful in programming. Since it prevents us
from re-writing the same code multiple times .
Once defined cn be invoked many times I the
programme makes the code reusable.
They make it modular, easy to manage and
understand.

Arguments and parameter – are self contained


modules that accomplish a specific task.
PROGRAMES

INPUT OUPTUT EXPLANATIO


N
#Identify Data types [1] "numeric" Class - helps
I=10 [1] "integer" determine the type of
class(I) [1] "character" object, such as a data
[1] "character" frame, numeric vector,
I=10L [1] "numeric" or character vector.
class(I) [1] "character"
[1] "logical" I – Numeric or double
C="NAME" -Integer
class(C) L suffix – denotes
long integer
S="WHAT IS YOUR NAME" C–
class(S) character :assigned
string
V1=c(1,2,3,4) V1 – numeric : all
class(V1) elements are numbers
V2 – mix data types:
V2=c(1,"RAM",TRUE) character
class(V2) V3 – has logical
values
V3=c(TRUE,FALSE)
class(V3)
D=data.frame(id=c(1,2,3,4),name=c("ram", 'data.frame': 4 $ - used for extracting,
"shyam","rahul","ramesh")) obs. of 2 variables: add, update, variables
str(D) $ id : num 1 2 3 4 from the list and data
$ name: Factor w/ 4 from columns.
levels "rahul","ram",..: 2
413

#Convert numeric to character [1] "numeric" As.character – allows


N=c(1,2,3,4) [1] "character" you to convert
class(N) numeric to character
N_CHAR=as.character(N)
class(N_CHAR)

#Convert character TO numeric [1] "character" As.numericAllows


CH=c("1","2","3","4") [1] "numeric" you to convert
class(CH) character to numeric
CH_NUM=as.numeric(CH)
class(CH_NUM)
#Basic arithmetic operations [1] 31 +: Combines values
[1] 11
10+21 [1] 210 -: to find the
[1] 2.1 difference between
21-10 [1] 1 values
[1] 100
10*21 [1] 31 DIVISION: To find
[1] 11 the ratios and
21/10 [1] 210 proportion
[1] 2.1
21%%10 [1] 1 Modulo
[1] 100
10^2 : calculates the
reminders of two
a=10 numbers (%%)
b=21
add=a+b
add

subs=b-a
subs

mul=a*b
mul

div=b/a
div

mod=b%%a
mod

pow=a^2
pow

#BUILD-IN FUNCTION [1] 15 SUM() – calculate the


[1] 3 sum of many numbers
A1=c(1,2,3,4,5) [1] 1 2 3 4 5 Mean() – average of
sum(A1) the elements
mean(A1) PRINT() – contents of
print(A1) the vector A1 to the
console.

[1] 13 Add_numbers –
add_numbers=function(a,b) [1] 31 computes the sum of
{c=a+b two input numbers
return(c)}

add_numbers(5,8)
add_numbers(10,21)
R2

Terms Explanation
Vector Collection of elements of the same data type. Ex: Numeric, character, logical.

Matrix 2D array elements of the same data type

Array Multi dimensional extension of matrix.

List Collection of elements that can have different data types.

Data frame Table like structure wit rows and columns and columns have different data
types.

Factors Represents categorical variables ex:factor(c(“red”,”bue”,Yellow”))

Tables Contingency tables – summarize the distribution of categorial variables

R Programming
Input Output Explanation
numeric_vector=c(1.5, 2.3, 0.7, [1] 1.5 2.3 0.7 -2.5 Numeric vector – used for
-2.5) storing data points.
numeric_vector

matrix_data=matrix(c(1, 2, 3, 4, Matrix can be represented in


5, 6), nrow = 2, ncol = 3) [,1] [,2] [,3] two or three ways
matrix_data [1,] 1 3 5
[2,] 2 4 6

array_data=array(c(1, 2, 3, 4, 5, Array – Compared to matrix,


6), dim = c(2, 3, 1)) ,,1 arrays can have more tan two
array_data dimensions.
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6

my_list=list(name = "John", List – ordered collection of


age = 25, grades = c(90, 85, $name heterogenous data elements.
92)) [1] "John" It can contain objects(numbers,
my_list characters, vectors, matrices,
$age and even other lists.
[1] 25 Components of list can be
named, and you use those
$grades names to access the elements
[1] 90 85 92 using the dollar sign.

df=data.frame(name = Data frame- is a versatile data


c("Alice", "Bob", "Charlie"), name age structure that allows you to
age = c(28, 35, 22)) 1 Alice 28 organize tabular data with
df 2 Bob 35 different types of variables.
3 Charlie 22 Commonly used for
manipulation and analysis of
data.

gender=factor(c("Male", Factor – Conert character


"Female", "Male", "Female")) [1] Male Female Male vector into factor.
gender Female Used to represent categorical
Levels: Female Male data, and its particularly useful
wen working with character
columns in data frames or
creatin statistical summaries
for categorical variables.

my_table=table(c("A", "B", Table is useful for summarizing


"A", "C", "B")) ABC categorical data and
my_table 221 understanding the distribution of
#View(my_table) values.
#manipulating data structures
[1] 2.3 To create , access, modify
# Creating a numeric vector [1] 1.5 2.3 10.1 -2.5 elements in a numeric vector.
numeric_vector=c(1.5, 2.3, 0.7,
-2.5)

# Accessing elements
numeric_vector[2] # Print the
second element

# Modifying elements
numeric_vector[3]=10.1
numeric_vector

# Creating a matrix [,1] [,2] [,3]


matrix_data=matrix(c(1, 2, 3, 4, [1,] 1 3 5
5, 6), nrow = 2, ncol = 3) [2,] 2 4 6
matrix_data [1] 6
# Accessing elements [,1] [,2] [,3]
matrix_data[2, 3] # Print the [1,] 1 8 5
element in the second row and [2,] 2 4 6
third column

# Modifying elements

matrix_data[1, 2]=8
matrix_data

# Creating a list [1] 25


my_list =list(name = "John", $name
age = 25, grades = c(90, 85, [1] "John"
92))
$age
# Accessing elements [1] 26
my_list$age # Print the age
$grades
# Modifying elements [1] 90 85 92
my_list$age = 26
my_list

# Creating a data frame


df=data.frame(name = [1] Alice Bob Charlie
c("Alice", "Bob", "Charlie"), Levels: Alice Bob Charlie
age = c(28, 35, 22)) name age
1 Alice 29
# Accessing columns 2 Bob 36
df$name # Print the 'name' 3 Charlie 23
column

# Modifying columns
df$age=c(29, 36, 23)
df
#indexing vectors
# Creating a vector
numeric_vector=c(1.5, 2.3, 0.7,
-2.5)
[1] 2.3
# Accessing elements by [1] 1.5 0.7
position [1] 1.5 2.3
numeric_vector[2] # Returns
the second element (2.3)

# Accessing multiple elements


numeric_vector[c(1, 3)] #
Returns the first and third
elements

# Using logical indexing


numeric_vector[numeric_vector
> 1] # Returns elements greater
than 1

#indexing matrices ans arrays

# Creating a matrix
matrix_data=matrix(1:9, nrow
= 3)
matrix_data [,1] [,2] [,3]
[1,] 1 4 7
# Accessing elements by row [2,] 2 5 8
and column [3,] 3 6 9
matrix_data[2, 3] # Returns the [1] 8
element in the second row and [1] 2 5 8
third column [1] 7 8 9
[1] 6 7 8 9
# Accessing entire rows or
columns
matrix_data[2, ] # Returns the
entire second row
matrix_data[, 3] # Returns the
entire third column

# Using logical indexing


matrix_data[matrix_data > 5] #
Returns elements greater than 5
#indexing data frames

# Creating a data frame [1] Alice Bob Charlie The index of a Data Frame is
df = data.frame(name = Levels: Alice Bob Charlie a series of labels that identify
c("Alice", "Bob", "Charlie"), [1] 28 35 22 each row. The labels can be
age = c(28, 35, 22)) name age integers, strings, or any other
2 Bob 35 hash able type.
# Accessing columns by name
df$name # Returns the 'name'
column

# Accessing columns by
position
df[, 2] # Returns the second
column (age)

# Using logical indexing


df[df$age > 30, ] # Returns
rows where age is greater than
30
R3

Terms Explanation
Getwd() This function is used to get current working
directory.
Setwd() This function is used to set the working
directory to a specified path.
Read.csv Function gives the output as a data frame.

1) #IMPORT DATA(CSV FILE)

MTCARS1=read.csv("mtcars.csv", header = TRUE)

MTCARS1

View(MTCARS1)

2) #EXPORT DATA (CSV FILES)


write.csv(MTCARS1,"MTCARS2.CSV")
str(MTCARS1)
dim(MTCARS1)
str(MTCARS1)
head(MTCARS1)
tail(MTCARS1)
length(MTCARS1)
3) #REVIEWING THE TABLE
names(MTCARS1)
MTCARS1$mpg
mean(MTCARS1$mpg)
mean(MTCARS1$hp)
summary(MTCARS1)
MTCARS1$cyl
table(MTCARS1$mpg)

4) # CREATING FREQUENCY TABLE


P=table(MTCARS1$cyl)
View(P)
5) # BAR PLOT
plot(P)
barplot(P, col = "GREEN",xlab = "cylinder", ylab = "frequency")

6) # Plotting Scatter Plot


plot(MTCARS1$mpg,MTCARS1$hp, col= rainbow(10))

7) # Plotting Boxplot
boxplot(MTCARS1$mpg)
boxplot(MTCARS1$mpg ~ MTCARS1$cyl)
#boxplot(mpg ~ cyl, data = MTCARS)
8) # Plotting Histogram
hist(MTCARS1$mpg,col=rainbow(15))

You might also like