0% found this document useful (0 votes)
4 views3 pages

Ass Soln2

The document provides an assignment solution for a course on Data Analytics with R, discussing the programming language R, its features, and its applications in statistical computing and data analysis. It covers topics such as lists, functions, DataFrames, and flow control statements in R, including examples of recursive functions and control structures. The content is aimed at students in the Department of Artificial Intelligence and Machine Learning at The Oxford College of Engineering, Bangalore.

Uploaded by

Shruti Vikas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Ass Soln2

The document provides an assignment solution for a course on Data Analytics with R, discussing the programming language R, its features, and its applications in statistical computing and data analysis. It covers topics such as lists, functions, DataFrames, and flow control statements in R, including examples of recursive functions and control structures. The content is aimed at students in the Department of Artificial Intelligence and Machine Learning at The Oxford College of Engineering, Bangalore.

Uploaded by

Shruti Vikas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Children’s Education Society ®

THE OXFORD COLLEGE OF ENGINEERING, BANGALORE -560068


(Approved by AICTE, New Delhi, Accredited by NAAC ‘A’, & Affiliated to VTU, Belagavi-
590 018)

Department of Artificial Intelligence and Machine Learning

Data Analytics With R


Assignment 1 Solution

1) Discuss about R and List the features of R


Soln: R is a programming language and software environment that is primarily used
for statistical computing and data analysis. It provides a wide variety of statistical
and graphical techniques, and is highly extensible, allowing users to develop their
own statistical methods and visualizations. R has become one of the most popular
tools among statisticians, data analysts, and researchers in various fields due to its
flexibility and power
Key Features of R:
 Open Source:
 Statistical and Graphical Analysis:
 Data Handling and Manipulation
 Integration with Other Languages:
 Reproducibility:

2) Demonstrate about the Lists and Functions in R.


Soln: A function and its environment together is called a closure. When we load a
package, the functions in that package are stored in the environment on the search
path where the package is installed. A function in R is a verb and not a noun as it
does things with its data. Functions are also another data types and hence we can
assign and manipulate and pass them as arguments to other functions. Typing the
function name in the command prompt lists the code associated with the function.
Below is the code listed for the function readLines().
Lists: Lists allow us to combine different data types in a single variable. Lists can be
created using the list() function. This function is similar to the c() function. The
contents of a list are just listed within the list() function as arguments separated by a
comma. List elements can be a vector, matrix or a function.

3) Develop a program to find the factorial of given number using recursive function
calls.
Soln: # Define a recursive function to calculate the factorial
factorial <- function(n) {
# Base case: if n is 0 or 1, the factorial is 1
if (n == 0 || n == 1) {
return(1)
}
# Recursive case: n * factorial of (n - 1)
else {
return(n * factorial(n - 1))
}
}

# Example: Input the number for which factorial is to be calculated


num <- as.integer(readline(prompt = "Enter a number to find its factorial: "))

# Calculate and print the factorial


result <- factorial(num)
cat("The factorial of", num, "is:", result, "\n")

4) Explain about DataFrames in R


Soln: DataFrame is a two-dimensional, tabular data structure that is commonly
used for storing and manipulating data. It is one of the most essential and powerful
data structures in R, especially when dealing with structured data such as datasets
from surveys, experiments, and other forms of organized data.
A DataFrame is similar to a table or a spreadsheet, where data is organized into rows
and columns. Each row represents an observation (e.g., an individual record), and
each column represents a variable or feature (e.g., age, gender, height). DataFrames
have both row names and column names, which makes them easy to reference and
manipulate. DataFrames support a wide range of operations like subsetting, merging,
reshaping, and sorting, which makes them easy to manipulate and analyze.
DataFrames can be easily converted into other R data structures, such as matrices or
lists, and vice versa, making it easy to work with different types of data.

5) Discuss any two Flow Control Statements with a Program`


Soln: The if statement takes a logical value and executes the next statement only if
the value is TRUE. It is not necessary to pass the logical values as TRUE or FALSE
directly, instead a variable or expression that returns a logical value can be used. If there are
several statements to execute after the condition, they can be wrapped in curly braces
In the if and else construct the code that follows the if statement is executed if the condition
is TRUE and the code that follows the else statement is executed if the condition is FALSE.
It is important to note that the else statement must occur on the same line as the closing
curly brace of the if statement and otherwise it will throw an error message

If there are many else statements, it looks confusing and in such cases the switch() function
is required. The first argument of the switch statement is an expression that can return a
string value or an integer. This is followed by several named arguments that provide the
results when the name matches the value of the first argument. Here also we can execute
multiple statements enclosed by curly braces. If there is no match the switch statement
returns NULL. So, in this case, it is safe to mention a default value if none matches

Faculty HOD

You might also like