Professional Documents
Culture Documents
a. Mention the list of commands used to clean up any of the object or for several
objects.
b. Explain in detail the treatment of missing values in R.
Ans.(a)
Data Cleaning is the process of transforming raw data into consistent data that can be
analyzed. It is aimed at improving the content of statistical statements based on the data as
well as their reliability.
rm() function in R Language is used to delete objects from the memory. It can be used with
ls() function to delete all objects. remove() function is also similar to rm() function.
Syntax: rm(x)
Parameters:
x: Object name
Example 1:
# R Program to remove
# objects from Memory
# Creating a vector
vec <- c(1, 2, 3, 4)
vec
# Creating a list
list1 = list("Number" = c(1, 2, 3),
"Characters" = c("a", "b", "c"))
list1
# Creating a matrix
mat <- matrix(c(1:9), 3, 3)
mat
# Calling rm() Function
rm(list1)
# Calling ls() to check object list
ls()
Output:
[1] 1 2 3 4
$Number
[1] 1 2 3
$Characters
[1] "a" "b" "c"
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9
[1] "mat" "vec"
Example 2:
# R Program to remove
# objects from Memory
# Creating a vector
vec <- c(1, 2, 3, 4)
# Creating a list
list1 = list("Number" = c(1, 2, 3),
"Characters" = c("a", "b", "c"))
# Creating a matrix
mat <- matrix(c(1:9), 3, 3)
# Calling rm() Function
# to remove all objects
rm(list = ls())
# Calling ls() to check object list
ls()
Output:
character(0)
Ans.(b)
As the name indicates, Missing values are those elements which are not known. NA or NaN
are reserved words that indicate a missing value in R Programming language for q
arithmetical operations that are undefined.
R – handling Missing Values
Missing values are practical in life. For example, some cells in spreadsheets are empty. If an
insensible or impossible arithmetic operation is tried then NAs occur.
Missing Values in R, are handled with the use of some pre-defined functions:
A logical vector is returned by this function that indicates all the NA values present. It returns
a Boolean value. If NA is present in a vector it returns TRUE else FALSE.
Output:
A logical vector is returned by this function that indicates all the NaN values present. It
returns a Boolean value. If NaN is present in a vector it returns TRUE else FALSE.
Output:
Output:
[1] NA 3 4 NA NA NA
Example 1:
Output:
[1] 1 2 3 4
Example 2:
x <- c(1, 2, 0 / 0, 3, NA, 4, 0 / 0)
x
x[! is.na(x)]
Output:
And hence this way the function calls one of the missing value filter functions. Missing Value
Filter Functions alter the data set and in the new data set the value of NAs has been changed.
The default Missing Value Filter Function is na.omit. It omits every row containing even one
NA. Some other Missing Value Filter Functions are:
# fails if NA is encountered
na.fail (df)
Output:
Factor Vectors– is the symbol displayed in factor vectors for missing values.
NaN – This is a special case of NA only. It is displayed when an arithmetic operation
yields a result which is not a number. For example, dividing zero by zero produces
NaN.