0% found this document useful (0 votes)
2 views8 pages

R_programming_assignment1

The document provides an overview of R programming, including its definition, characteristics, and applications in statistical analysis, data science, and machine learning. It also covers RStudio as an IDE for R, basic data types, key data structures, and examples of R objects such as vectors, lists, matrices, arrays, factors, and data frames. Additionally, it discusses loops in R and their syntax, highlighting the differences between various data structures.
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)
2 views8 pages

R_programming_assignment1

The document provides an overview of R programming, including its definition, characteristics, and applications in statistical analysis, data science, and machine learning. It also covers RStudio as an IDE for R, basic data types, key data structures, and examples of R objects such as vectors, lists, matrices, arrays, factors, and data frames. Additionally, it discusses loops in R and their syntax, highlighting the differences between various data structures.
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

R- Programming

Unit -I & II
Q1 What is R, and what are its main characteristics?
R is a free, open-source programming language and software environment
specifically designed for statistical computing, data analysis, and high-quality data
visualization. Developed by Ross Ihaka and Robert Gentleman in the early 1990s,
it is widely used by researchers, statisticians, and data scientists for data
manipulation, modeling, and machine learning
Q 2 What is RStudio?
RStudio is a popular, free, and open-source Integrated Development Environment
(IDE) designed specifically for the R programming language, facilitating data
analysis, visualization, and statistical computing. It provides a user-friendly
interface with a syntax-highlighting editor, workspace management, and
debugging tools, available for Windows, Mac, and Linux. RStudio does not replace
R; it requires R to be installed separately
Q 3 Define some basic data types in R.
R has six basic data types: numeric, integer, logical, character, complex, and raw.
These types are the building blocks for more complex data structures like vectors,
lists, and data frames
Q 4 What are the different data objects in R- Programming?
In R, everything is an object. These objects are categorized by their
underlying data types and their organizational data structures
Types of Objects: R supports various objects like vectors, lists, data frames,
matrices and functions. Memory Allocation: Unlike many languages, where
memory is allocated based on explicit data types, R assigns memory dynamically
when objects are created or modified
Q 5 Write Applications of R Programming

R is a programming language and software environment for statistical analysis, graphics


representation and reporting. Following are applications of R programming language.

Statistical Analysis − R programming provides inbuilt support for descriptive statistics,


inferential statistics, regression analysis, time series analysis and classical statistical tests.

Data Science − R is very important tool in data science workflow and supports data wrangling
and preprocessing, data exploration and data import/export activities.

Data Visualization − Using R, we can easily create static graphics, interactive visualizations, and
maps.

Machine Learning − R provides a rich environment for supervised learning, unsupervised


learning, model evaluation, model selection and so on.

Academia and Research − R is a preferred language in many premier academic and research
institutions for teaching statistics and data science and to publish researches.

Q 6 Define some basic data types in R.


R has six basic data types: numeric, integer, logical, character, complex, and raw.
These types are the building blocks for more complex data structures like vectors,
lists, and data frames

Q 7 How to assign a value to a variable in R?


# Assigning a numeric value
my_number <- 42
# Assigning a character string
my_text <- "Hello, R!"
# Assigning a logical value
is_true <- TRUE
Q 8 What are the key data structures in R?
The key data structures in R programming are vectors, lists, matrices, arrays, data
frames, and factors. They can be broadly classified by their dimensionality and
whether they store homogeneous (same type) or heterogeneous (different types)
data.
Q 9 Write executing of basic R scripts
RStudio is the most common environment for working with R.
Executing basic R scripts can be done either within an Integrated Development
Environment (IDE) like RStudio or directly from your system's command line or
terminal. The R code itself is written in a plain text file, typically saved with a .R
file extension

Q 10 Write Applications of R- programming

• Data Analysis
• Machine Learning
• Statistical Modeling
• Data Visualization

Q 11 Write Features of R – Programming


As stated earlier, R is a programming language and software environment for statistical
analysis, graphics representation and reporting. The following are the important features of R −

R is a well-developed, simple and effective programming language which includes conditionals,


loops, user defined recursive functions and input and output facilities.

R has an effective data handling and storage facility,

R provides a suite of operators for calculations on arrays, lists, vectors and matrices.

R provides a large, coherent and integrated collection of tools for data analysis.

R provides graphical facilities for data analysis and display either directly at the computer or
printing at the papers.
Q 12 Write a R-Program to find out Factorial of a Number
Q 13 Write a R-Program to find out largest number from 3 numbers

Q 14 Describe types of R-Objects found in R- Programming with


example
In contrast to other programming languages like C and java in R, the variables are not
declared as some data type. The variables are assigned with R-Objects and the data type of
the R-object becomes the data type of the variable. There are many types of R-objects. The
frequently used ones are −

Vectors
Lists
Matrices
Arrays
Factors
Data Frames

Vectors --->
When you want to create vector with more than one element, you should use c() function which
means to combine
the elements into a vector.

# Create a vector.
apple <- c('red','green',"yellow")
print(apple)

# Get the class of the vector.


print(class(apple))

[1] "red" "green" "yellow"


[1] "character"

Lists ---->

A list is an R-object which can contain many different types of elements inside it like vectors,
functions and even another list inside it.
# Create a list.
list1 <- list(c(2,5,3),21.3,sin)
print(list1)

[1] 2 5 3
[[2]]
[1] 21.3
[[3]]
function (x) .Primitive("sin")

Matrices --- >


A matrix is a two-dimensional rectangular data set. It can be created using a vector input to the matrix
function.

# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)

[,1] [,2] [,3]


[1,] "a" "a" "b"
[2,] "c" "b" "a"

Arrays --
While matrices are confined to two dimensions, arrays can be of any number of dimensions. The array
function takes a dim attribute which creates the required number of dimension. In the below example
we create an array with two elements which are 3x3 matrices each.

# Create an array.
A <- array (c ('green','yellow'), dim = c(3,3,2))
print(a)
[,1] [,2] [,3]
[1,] "green" "yellow" "green"
[2,] "yellow" "green" "yellow"
[3,] "green" "yellow" "green"

[,1] [,2] [,3]


[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"

Factors -
Factors are the r-objects which are created using a vector. It stores the vector along with the distinct
values of the elements in the vector as labels. The labels are always character irrespective of whether
it is numeric or character or Boolean etc. in the input vector. They are useful in statistical modeling.

Factors are created using the factor() function. The nlevels functions gives the count of levels.

# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')
# Create a factor object.
factor_apple <- factor(apple_colors)
# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))

[1] green green yellow red red red green


Levels: green red yellow
[1] 3
Data Frames -
Data frames are tabular data objects. Unlike a matrix in data frame each column can contain different
modes of data. The first column can be numeric while the second column can be character and third
column can be logical. It is a list of vectors of equal length.

Data Frames are created using the [Link]() function.

# Create the data frame.


BMI <- [Link] (
gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)

gender height weight Age


1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26

Q 15 What is the main difference between an Array and a matrix?


In R programming, the main difference is that a matrix is a special case of an array
that is strictly two-dimensional, while a general array can have one or more
dimensions
Q 16 Give the general expression to create a matrix in R.

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)

Q 17 Write a R program to show Matrix with examples


In R, a matrix is a two-dimensional data structure containing elements of the
same data type. You can create a matrix using the matrix() function, or by
combining vectors using cbind() or rbind()
Q. 18 Write different between Vectors and List in R- Programming
Q. 19 Write different between Matrix and Arrays in R- Programming
Q. 20 Write different between Factors and Data Frames in R- Programming .

Q 21 What types of loops exist in R, and what is the syntax of each type?
1. For loop—iterates over a sequence the number of times equal to its length (unless the
statements break and/or next are used) and performs the same set of operations on each item
of that sequence. This is the most common type of loops. The syntax of a for loop in R is the
following:

for (variable in sequence) {


operations
}
2. While loop—performs the same set of operations until a predefined logical condition (or
several logical conditions) is met—unless the statements break and/or next are used. Unlike
for loops, we don't know in advance the number of iterations a while loop is going to execute.
Before running a while loop, we need to assign a variable (or several variables) and then update
its value inside the loop body at each iteration. The syntax of a while loop in R is the following:

variable assignment
while (logical condition) {
operations
variable update
}

You might also like