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

SMuR Assignment

Uploaded by

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

SMuR Assignment

Uploaded by

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

Problem Statement:

Perform basic operations and manipulations in R, including installing R and


RStudio, exploring basic syntax, and understanding data types.

Introduction:
R is a statistical computing and graphics system. This system is comprised of
two parts: the R language itself (which is what most people mean when they
talk about R) and a run-time environment.
R is an interpreted language, which means that users access its functions
through a command-line interpreter.
Unlike languages such as Python and Java, R is not a general-purpose
programming language. Instead, it’s considered a domain-specific language
(DSL), meaning its functions and use are designed for a specific area of use or
domain.
In R’s case, that’s statistical computing and analysis. By extension, R is
commonly used for all manner of data science tasks.
R is equipped with a large set of functions that enable data visualizations, so
users can analyse data, model it as required, and then create graphics. In
addition to the language’s in-built graphical functions, there are numerous
add-ons or modules that facilitate this.
Is R a low or high-level language?

R is considered a high-level programming language. This classification is


based on its level of abstraction from machine language. Unlike low-level
languages that require in-depth knowledge of computer memory and
processes, high-level languages like R are designed to be easily understood
and written by humans, making them more accessible for statisticians, data
analysts, and researchers.
R offers power, extensibility, and flexibility in droves, but the ‘cost’ when
compared to languages like Python is a certain level of complexity.
R is not the easiest programming language to learn, but it’s also not as
difficult as many would have you believe.
Some basic data types in R:
• Character: Used to specify character or string values in a variable. For
example, "A" is a single character and "Apple" is a string.
• Numeric: Can be real or decimal. For example, 2, 2.0, or pi.
• Integer: Can be declared as an integer by using the letter "L". For
example, 1L, 55L, or 100L.
• Complex: Can be represented as complex numbers. For example, 9 + 3i,
where "i" is the imaginary part.
• Logical: Can be represented as TRUE or FALSE

R Operators:

An Operator is a symbol that tells to perform different operations between


operands. R programming is very rich in built-in operators.

R has the following data operators:

• Arithmetic Operators
• Assignment Operators
• Logical Operators
• Relational Operators
• Miscellaneous Operators

Let us discuss the types of operators in detail.

Arithmetic Operators

These operators perform basic arithmetic operations like addition, subtraction,


multiplication, division, exponent, modulus, etc.

Let us see what these operators do.

For example, take two variables:

For example:-
1x <- 10
2y <- 5
Operator Operation Output

x+y Addition 15

x–y Subtraction 5

x*y Multiplication 50

x/y Division 2

x^y Exponent 10^5

x %% y Modulus 0

These operators can also be used to carry out mathematical operations on


vectors.

For creating vectors, we use the c() function.

In the case of vectors, all these operations are done in an element-by-element


fashion.

For example:
1x <- c(9,9,9)
2y <- c(1,1,1)
3print(x+y)
4Output: [1] 10 10 10

Let us see how you can use these arithmetic operators in R programming.

• Addition

a <- c(8,4.2,7)

b <- c(2, 4, 3)
print(a+b)

Output:

[1] 10.0 8.2 10.0

• Subtraction

a <- c(4,7.8,7)

b <- c(6, 4, 2)

print(a-b)

Output:

[1] -2.0 3.8 5.0

• Multiplication

a <- c(4,6.2,7)

b <- c(8, 2, 6)

print(a*b)

Output:

[1] 32.0 12.4 42.0

• Division

a <- c(6,5.5,8)

b <- c(12, 2, 4)

print(a/b)

Output:

[1] 0.50 2.75 2.00


• Exponent

a <- c(3,5.5,4)

b <- c(2, 4, 3)

print(a^b)

Output:

[1] 9.0000 915.0625 64.0000

• Modulus

a <- c(3,7.5,8)

b <- c(6, 4, 4)

print(a%%b)

Output:

[1] 3.0 3.5 0.0

Assignment Operators:

The use of these operators is to assign values to the variables. There are two
kinds of assignments, leftwards assignment, and rightwards assignment.
Operators ‘<-‘ and ‘=’ are used to assign values to any variable.
x<- 3 or x = 3 (Leftwards Assignment)
3 -> x or x = 3

Logical Operators:

These operators are used to perform Boolean operations like AND, OR, NOT,
etc.
Different logical operators are as follows:

! NOT

& AND (Element wise)

&& AND

| OR (Element wise)

|| OR

NOT
! –

ZEROS are taken as FALSE and NON-ZERO numbers are taken as TRUE.
For example:
1x <- c (FALSE, TRUE,3,0)
2y <- c (FALSE, TRUE, FALSE, TRUE)
3!x (NOT operation)
4x&y (AND operation)
5Output:
6[1] TRUE FALSE FALSE TRUE
7[1] FALSE TRUE FALSE FALSE
8and,x&&y
9x||y
10Output:
11[1] FALSE
12[1] FALSE
Relational Operators:

These operators are used to compare two values or variables. To find if one is
smaller, greater, equal, not equal, and other similar operations these operators
are used. The output of a
A relational operator is always a Logical value, that is either TRUE or FALSE.
For example:-
x <- 10
y <- 5
Greater than x>y Output: TRUE

Less than x<y Output: FALSE

Greater than and equal to x>=y Output: TRUE

Less than and equal to x<=y Output: FALSE

Equal to x==y Output: FALSE

Not equal to x!=y Output: TRUE


Perform basic operation:

You might also like