R _WEEK-2
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"
TEST CASE :
SOURCE CODE : Character
TEST CASE :
TEST CASE :
TEST CASE :
TEST CASE :
TEST CASE :
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
Logical Operators:
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:
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