Professional Documents
Culture Documents
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.
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.
subs=b-a
subs
mul=a*b
mul
div=b/a
div
mod=b%%a
mod
pow=a^2
pow
[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.
Data frame Table like structure wit rows and columns and columns have different data
types.
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
# Accessing elements
numeric_vector[2] # Print the
second element
# Modifying elements
numeric_vector[3]=10.1
numeric_vector
# Modifying elements
matrix_data[1, 2]=8
matrix_data
# 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)
# 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
# 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)
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.
MTCARS1
View(MTCARS1)
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))