[go: up one dir, main page]

0% found this document useful (0 votes)
21 views20 pages

BRM File

Uploaded by

garvbarreja
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)
21 views20 pages

BRM File

Uploaded by

garvbarreja
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/ 20

Ques1.

Introduction to R-Studio
Ans2.
 R is one of the programming languages that provide an
intensive environment for you to research, process,
transform, and visualize information.
 Many users of the R programming language like the fact
that it is free to download, offers sophisticated data
analytics capabilities, and has an active community of
users online where they can turn to for support.
 It has 9000+ packages.
 It is mainly used for complex data analysis in data science.

Ques 2. Types of Data in R. Studio


Ans2.
According to r-studio the four main types of data most likely to
be used are: -
1. Numeric data
2. Character data (string like x)
3. Date data (time-based)
4. Logical data (true/false)
Ques3. Defining and Assigning Values
to Variables
Ans3.
Storing a value or “assigning” it to a variable is completed using either <-, = or - >
function.
The name given to a variable should describe the information or data being
stored.
This helps when revisiting old code or when sharing it with others.
> num1=2
> name="Drishti”
> fee paid=TRUE

Ques4. R Operators
Ans4.
 Arithmetic operators: The R arithmetic operators allow us to do math
operations, like sums, divisions, or multiplications, among others.
 Logical/Boolean operators: In addition, Boolean or logical operators in R
are used to specify multiple conditions between objects. These
comparisons return TRUE and FALSE values.
 Relational/Comparison operators: Comparison or relational operators are
designed to compare objects like Greater than (>), Less than (=), Less than
equal to (<=), Equal to (=).
 Assignment operators: The assignment operators in R allow you to assign
data to a named object in order to store the data like (<-)Left assignment, (-
>)Right assignment.
Ques5. Vectors in R Studio
Ans5.
In R, a sequence of elements that share the same data type is known as
a vector. If we use only one item like 2 then it is variable but if a
number of items are calculated collectively it is called a vector.
> vec1=c(1,2,3,4,5,6)
> vec1
[1] 1 2 3 4 5 6
> class(vec1)
[1] "numeric"
> vec2=c("a","b","c","d")
> vec2
[1] "a" "b" "c" "d"
> class(vec2)
[1] "character"
> vec3=c(T,F,T,F)
> vec3
[1] TRUE FALSE TRUE FALSE
> class(vec3)
[1] "logical"
> vec4=c(1,"a",2,"b",3)
> vec4
[1] "1" "a" "2" "b" "3"
> class(vec4)
[1] "character"
> vec5=c(1,T,2,F,3,T)
> vec5
[1] 1 1 2 0 3 1
> class(vec5)
[1] "numeric"
> vec6=c("a",T,"b",F,"c")
> vec6
[1] "a" "TRUE" "b" "FALSE" "c"
> class(vec6)
[1] "character"
> vec7=c(1,"a",T,2,"b”,F)
> vec7
[1] "1" "a" "TRUE" "2" "b" "FALSE"
> class(vec7)
[1] "character"
Ques6. Lists in R Studio
Ans6.
Lists are the R objects which contain elements of different types like −
numbers, strings, vectors, and other lists inside them. The list is created
using list() function.
> 11=list(1,"a",TRUE)
> 11
[[1]]
[1] 1
[[2]]
[1] "a"
[3]]
[1] TRUE
> class(l1[[1]])
[1] "numeric"
> class(l1[[2]])
[1] "character"
> class(l1[[3]])
[1] "logical”
List of Vectors
> l2=list(c(1,2,3),c("a","b","c"),c(T,F,T))
> l2
[[1]]
[1] 1 2 3
[[2]]
[1] "a" "b" "c"
[[3]]
[1] TRUE FALSE TRUE
> 12[[2]][1] [1] "a"
> l2[[1]][3]
[1] 3

Ques7. Matrices in R Studio


Ans7.
In R, a two-dimensional rectangular data set is known as a matrix. A
matrix is created with the help of the vector input to the matrix
function. On R matrices, we can perform addition, subtraction,
multiplication, and division operation.
In the R matrix, elements are arranged in a fixed number of rows and
columns. The matrix elements are the real numbers.
>m1=matrix(c(1,2,3,4,5,6))
>m1
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
>m1=matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)
>m1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> m1=matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=T)
> m1
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
> m1[1,2]
[1] 2
Ques8. Array in R Studio
Ans8.
An array is a data structure that can hold multi-dimensional data. In R,
the array objects can hold two or more two-dimensional data. Arrays
are also called vector structures. A vector is an array of numbers with a
single index while a matrix is an array of numbers with two indices.
Uni-dimensional arrays are called vectors with the length being their
only dimension.
Two-dimensional arrays are called matrices, consisting of fixed numbers
of rows and columns.
Arrays consist of all elements of the same data type.
An array in R can be created with the use of array() function.
> vec1=c(1,2,3,4,5,6)
> vec2=c(7,8,9,10,11,12)
> a1=array(c(vec1,vec2),dim=c(2,3,2))
> a1
,,1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
,,2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
> a1[1,2,1]
[1] 3
> a1[1,1,2]
[1] 7
> a1[2,3,2]
[1] 12

Ques9. Data Frame in R Studio


Ans9.
>fruits=data.frame(fruit_name=c("apple","orange","litchi"),fruit_cost=c
(10,20,40))
> fruits
fruit_name fruit_cost
1 apple 10
2 banana 20
3 mango 30
> fruits$fruit_cost
[1] 10 20 40
> fruits$fruit_name
[1] "apple" "orange" "litchi"

Ques10. User defined function in R


Ans10.
A function is a set of statements organized together to perform a specific task. R has a large
number of in-built functions and the user can create their own functions.
We can create user-defined functions in R. They are specific to what a user wants and once
created they can be used like the built-in functions.
The user-defined functions can be explained as below:
> new.function=function(a){for (i in 1:a) {b=i^2
print(b)}}
> new.function(6)
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
> new.function=function(a,b,c)
{result=a*b+c
print(result)}
> new.function(2,3,4)
[1] 10
> new.function(5,2,4)
[1] 14
> new.function(a=7,b=5,c=3)
[1] 38
By doing this we have generated our own function in R studio which is a*b+c, we can also
generate many other functions like this to increase the efficiency of our work and save time.

Ques11. If Else Statement in R Studio


Ans11.
IF STATEMENT:
As we can see in the tree chart, there are only two possible outcomes.
If Team A wins, they go to the playoffs. If Team B wins, then they go.
> teamA=5
> teamB=3
> if(teamA>teamB)
{print("Team A Wins")}
[1] "Team A Wins"
R will write Team A wins Because it is true as 5 is more than 3
ELSE STATEMENT:
What if Team A had 1 goal and Team B had 3 goals? Our team_A >
team_B conditional would evaluate to FALSE. As a result, nothing would
be printed if we ran our code. Because the if statement evaluates to
false, the code block inside the if statement is not executed. In this case
use the else statement with if:
>teamA=1
> teamB=3
>if(teamA>teamB)
{print("Team A Wins")}else{print("Team B wins")}
[1] "Team B wins"

Ques12.For Loop in R Studio


Ans12.
It is a type of control statement that enables one to easily construct a loop that
has to run statements or a set of statements multiple times. For loop is commonly
used to iterate over items of a sequence.
For(value in sequence){statement}
> for(val in 1:5){print(val)}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Ques13. While loop in R Studio


Ans13.
It is a type of control statement which will run a statement or a set of statements
repeatedly unless the given condition becomes false. A while loop in R is a close
cousin of the for loop in R. However, a while loop will check a logical condition,
and keep running the loop as long as the condition is true.
While(condition){statement}.
 If the condition in the while loop in R is always true, the while loop will be
an infinite loop, and our program will never stop running.

QUES14. Inbuilt Functions of R


Studio
Ans14.
>View(iris)
Show Data of Data Frame

>str(iris)
It will show the structure of Data Frame

'data.frame': 150 obs. of 5 variables:

$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9...

$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...

$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...

$ Species : Factor w/3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

> head(iris)-Show top 6 records Sepal.


Length Sepal.Width Petal.Length Petal.Width Species

1 5.1 3.5 1.4 0.2 setosa

2 4.9 3.0 1.4 0.2 setosa

3 4.7 3.2 1.3 0.2 setosa

4 4.6 3.1 1.5 0.2 setosa

5 5.0 3.6 1.4 0.2 setosa

6 5.4 3.9 1.7 0.4 setosa

> tail(iris) – Show last 6 records Sepal.


Length.Sepal.Width.Petal.Length.Petal.Width Species

145 6.7 3.3 5.7 2.5 virginica

146 6.7 3.0 5.2 2.3 virginica

147 6.3 2.5 5.0 1.9 virginica

148 6.5 3.0 5.2 2.0 virginica

149 6.2 3.4 5.4 2.3 virginica

150 5.9 3.0 5.1 1.8 virginica

> table(iris$Species)
It will show frequency of the field.

setosa versicolor virginica

50 50 50

> min(iris$Sepal.Length)
[1] 4.3

It will show the minimum value of a field.

> max(iris$Sepal.Length)
[1] 7.9
It will show the maximum value of a field.

> range((iris$Sepal.Length))
[1] 4.3 7.9

It will show the range (min & max) of values of a field.

> mean(iris$Sepal.Length) )
[1] 5.843333

It will show the average of filed.

Ques15. Mean, Mode and Mode in R


Studio
Ans15.
>marks=c(8,10,12,15,20,7,6,5,8,3,2,12,8,9,7,15,8)
Mean:
> mean(marks)
[1] 9.117647

> mean(iris$Sepal.Length)
[1] 5.843333

Median:
>median(marks)
[1] 8

>median(iris$Sepal.Length)
[1] 5.8

>table(marks)
It will show variable and its frequency.
Marks

2 3 5 6 7 8 9 10 12 15 20
1 1 1 1 2 4 1 1 2 2 1

Mode:
>names(sort(-table(marks))[1])
[1] "8"

>names(sort(-table(iris$Sepal.Length))[1])
[1] "5"

>median(iris$Sepal.Length)
>5.8

Ques16. Summary Statistics in R


Ans16.
 R provides a wide range of functions for obtaining summary statistics. One
method of obtaining descriptive statistics is to use the summary(file name)
function with a specified summary statistic.
 For this we first need to install a package in r studio named: Fbasics
 It helps you to calculate the descriptive statistics of the whole data series,
the values calculated by this are:
o Mean
o Median
o Minimum
o Maximum
o 1st and 3rd quartile
> marks=c(8,10,12,15,20,7,6,5,8,3,2,12,8,9,7,15,8)
> summary(marks)
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.000 7.000 8.000 9.118 12.000 20.000
> summary(iris$Sepal.Length)
Min. 1st Qu. Median Mean 3rd Qu. Max.
4.300 5.100 5.800 5.843 6.400 7.900

Ques17. Plot in R Studio


Ans17.
 For presenting the data in the form of simple plots we just need to write a
command plot(row name).
 This will help to draw a simple basic level plot of the data file selected,
which looks like this:
>marks=c(4,8,9,10,11,12,13,14,15,16,17,18,19,20,22,23,24,25)

>plot(marks)

Colored Quick plots:-


• We can also get the colored version of our plots for this the command adds the color element to it,
and the command is plot(row name, col=1)

•Color codes: 1-Black

2-Red

3-green

4-Blue

5-Aqua

6- Pink

>marks=c(8,10,12,15,20,7,6,5,8,3,2,12,8,9,7,15,8)
>plot(marks,col=2)

Ques18.Histogram in R Studio
Ans18.
 A histogram is a graph that shows the frequency of numerical data using rectangles.

 The height of a rectangle (the vertical axis) represents the distribution frequency of a variable (the
amount, or how often that variable appears).

 The width of the rectangle (horizontal axis) represents the value of the variable (for instance, minutes,
years, or ages).

 The histogram displays the distribution frequency as a two-dimensional figure, meaning the height
and width of columns or rectangles have particular meanings and can vary. A bar chart is a one-
dimensional figure. The height of its bars represents something specific.

 To draw a histogram using the command hist(data file)

 To draw the colored histogram use the command hist(data file, col(“Red”))

 To add labels to the horizontal axis use the command (xlab=) in the above command.

 To add a heading to the histogram using the commanding main() in the above command.

> marks=c(8,10,12,15,20,7,6,5,8,3,2,12,8,9,7,15,8)
> hist(iris$Sepal.Length)
Ques19. Pie Chart in R Studio
Ans19.
 A pie chart (or a circle chart) is a circular statistical graphic, which is divided into
slices to illustrate numerical proportions.
 In a pie chart, the arc length of each slice (and consequently its central angle
and area) is proportional to the quantity it represents.
 Pie charts are created with the function pie(x, labels=) where x is a non-negative
numeric vector indicating the area of each slice and labels= notes a character
vector of names for the slices.
> slices <- c(10, 12,4, 16, 8)
> lbls <- c("US", "UK", "Australia", "Germany", "France")
> pie(slices, labels = lbls, main="Pie Chart of Countries"

You might also like