0% found this document useful (0 votes)
6 views

R _WEEK-2

This document provides an overview of basic R programming concepts, including data types such as numeric, integer, complex, character, and logical. It also covers variables, operators (arithmetic, assignment, comparison, logical, and miscellaneous), and includes test cases for practical understanding. The document serves as a foundational guide for learning data analytics using R.
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)
6 views

R _WEEK-2

This document provides an overview of basic R programming concepts, including data types such as numeric, integer, complex, character, and logical. It also covers variables, operators (arithmetic, assignment, comparison, logical, and miscellaneous), and includes test cases for practical understanding. The document serves as a foundational guide for learning data analytics using R.
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

-

DATA ANALYTICS USING R


WEEK 2

Name: Roll.No:
Section: Date:

PROBLEM STATEMENT : Learn all the basics of R-Programming (Data types, Variables,
Operators etc.
Basic Data Types
Basic data types in R can be divided into the following types:
 numeric - (10.5, 55, 787)
 integer - (1L, 55L, 100L, where the letter "L" declares this as an integer)
 complex - (9 + 3i, where "i" is the imaginary part)
 character (a.k.a. string) - ("k", "R is exciting", "FALSE", "11.5")
 logical (a.k.a. boolean) - (TRUE or FALSE)
We can use the class() function to check the data type of a variable:
# numeric
x <- 10.5
class(x)
TEST CASE:
> # numeric
> x <- 10.5
> class(x)
[1] "numeric"

# integer
x <- 1000L
class(x)
TEST CASE:
# integer
x <- 1000L
class(x)
[1] "integer"

# complex
x <- 9i + 3
class(x)
TEST CASE:
# complex
x <- 9i + 3
class(x)
[1] "complex"
# character/string
x <- "R is exciting"
class(x)
TEST CASE:
# character/string
x <- "R is exciting"
class(x)
[1] "character"
# logical/boolean
x <- TRUE
TEST CASE:
# logical
x <- TRUE
class(x)
[1] "logical"

SOURCE CODE: Numeric

TEST CASE :
SOURCE CODE : Character

TEST CASE :

SOURCE CODE : Logical

TEST CASE :

SOURCE CODE : Factor

TEST CASE :

SOURCE CODE : Vector

TEST CASE :

SOURCE CODE : List


numeric_var=c(10,20,30)
char=c("a","b","c")
TEST CASE :

SOURCE CODE : Matrixs

TEST CASE :

SOURCE CODE : Data frames

TEST CASE :

Variables
Variables are containers for storing data values.
# using equal to operator
var1 = "hello"
print(var1)
[1]”hello”
# using leftward operator
var2 <- "hello"
print(var2)
[1] "hello"
# using rightward operator
"hello" -> var3
print(var3)
[1] "hello"

TEST CASE :

Operators:
R divides the operators in the following groups:
 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Miscellaneous operators

Arithmetic Operators:
Arithmetic operators are used with numeric values to perform common mathematical operations:

TEST CASE :

Relational Operators:
Relational operators are used to compare two values:
TEST CASE :

Operator Description

& Element-wise Logical AND operator. Returns TRUE if both elements are TRUE

&& Logical AND operator - Returns TRUE if both statements are TRUE

| Elementwise- Logical OR operator. Returns TRUE if one of the statements is


TRUE

|| Logical OR operator. Returns TRUE if one of the statements is TRUE

! Logical NOT - Returns FALSE if statement is TRUE

Logical Operators:

Logical operators are used to combine conditional statements

TEST CASE :

Assignment Operators:
Assignment operators are used to assign values to variables:
my_var <- 3
my_var <<- 3
3 -> my_var
3 ->> my_var
my_var # print my_var
TEST CASE:
> my_var <- 3
> my_var <<- 3
> 3 -> my_var
> 3 ->> my_var
> print(my_var)
[1] 3

Miscellaneous Operators:
Miscellaneous operators are used to manipulate data:

Operator Description Example

: Creates a series of numbers in a sequence x <- 1:10

%in% Find out if an element belongs to a vector x %in% y

%*% Matrix Multiplication x <- Matrix1 %*%


Matrix2

a<-1:10
print(a)
TEST CASE:
> a<-1:10
> print(a)
[1] 1 2 3 4 5 6 7 8 9 10

a=c(10,4,8)
b=c(10,2,7)
print(b%in%a)
TEST CASE:
> a=c(10,4,8)
> b=c(10,2,7)
> print(b%in%a)
[1] TRUE FALSE FALSE

a=c(10,4,8)
b=c(10,2,7)
print(b%*%a)
TEST CASE:
> a=c(10,4,8)
> b=c(10,2,7)
> print(b%*%a)
[,1]
[1,] 164

You might also like