1 Basic of R Program & Basic Mathematical Operations

Code:

print('Hello R Program')

x <- 10

y <- 20

z <- x + y

z

print(z)

18 + 4

18 - 4

18 * 4

4 - 18


4.5 + 2

4.5 - 2

4.5 * 2


10 / 2

4.5 / 3


2 ** 3  # Exponential

2 ^ 3   # Exponential

3.5 ** 2

3.5 ^ 2

10.0 ** 2

10.0 ^ 2

3.0 ** 3

3.0 ^ 3

2 ** 1.5

2 ^ 1.5

9 ** 0.5

9 ^ 0.5

9 ^ .5


9 ^ 1/2

9 ^ (1/2)


sqrt(9)


# operators with in %% are special operators 

8 / 3

8 %/% 3     # Integer Division

-8 %/% 3    # Integer Division

8 %/% -3    # Integer Division


14 %% 4     # Modulus 

14.75 %% 4  # Modulus 

10 %% 3.5   # Modulus 



TRUE + TRUE

TRUE + FALSE

TRUE * TRUE

TRUE * FALSE


# Not Possible

'ghg' + 3

'5' + 3

3 + '5'

'hk' * 3


Data Type and variable 

age = 24

age <- 24

marks <- 78

marks1 <- 78

subject_java <- 85

subject.java <- 85

subject_1 <- 75

subject.1 <- 75

#1subject <- 75

#_subject <- 75

#$subject <- 75


x = 10

typeof(x)

mode(x)

storage.mode(x)

class(x)


typeof(10.25)

mode(10.25)

storage.mode(10.25)

class(10.25)


typeof(TRUE)

mode(TRUE)

storage.mode(TRUE)

class(TRUE)


typeof('Hello')

mode('Hello')

storage.mode('Hello')

class('Hello')


x <- 10.25
x
class(x)

y <- 10
y
class(y)

is.integer(x)
is.integer(y)

y <- as.integer(10)
y
is.integer(y)
class(y)

y <- 10L
is.integer(y)

y <- as.integer(10.25)
y
is.integer(y)

y <- as.integer('5.75')
y
is.integer(y)

y <- as.integer('John')
y
is.integer(y)

z <- 3 + 4i
z
class(z)

#Logical
x <- TRUE
x
class(x)

x <- T
x
class(x)

#Character
x <- 'Jack'
x
class(x)

x <- '3.75'
x
class(x)

x <- as.character(3.75)
x
class(x)

Variable Assignment

age = 24
age <- 24         # Left Assignemnt
assign('age',24)
18 -> age         # Right Assignemnt

z <- y <- x <- 10
x
y
z

x = pi
x
x = letters
x
x = LETTERS
x
x = month.name
x
x = month.abb
x

Relational Operators 
x <- 10
y <- 20

x < y
x <= y
x > y
x >= y
x == y
x != y

x = 'Hello'
y = 'Program'
x < y
x <= y
x > y
x >= y
x == y
x != y

x = TRUE
y = FALSE
x < y
x <= y
x > y
x >= y
x == y
x != y


200 < 'Hello'
'Hello' > 20
10 < TRUE
FALSE > 20
'Hello' > TRUE
TRUE > 'Hello'

10 < 20 < 30

Logical Operators 

x <- TRUE
y <- TRUE

x & y

TRUE & TRUE
TRUE & FALSE
FALSE & TRUE
FALSE & FALSE

TRUE && TRUE  # Not Vectorized
TRUE && FALSE
FALSE && TRUE
FALSE && FALSE

TRUE | TRUE
TRUE | FALSE
FALSE | TRUE
FALSE | FALSE

TRUE || TRUE   # Not Vectorized
TRUE || FALSE
FALSE || TRUE
FALSE || FALSE

!TRUE
!FALSE

10 & TRUE
FALSE & 20
10 | TRUE
FALSE | 20
10 | TRUE
FALSE | 20
!10
!0
!-10

#Cannot Use
'Hello' & 'Program'
'Hello' | 'Program'
!'Hello'
10 & 'Hello'
'Hello' & 20
'Hello' & TRUE
TRUE & 'Hello'

Sequence in R

x <- 1:10
x

x <- 5:1
x

x <- 2 * 1:10
x

x <- 10
1:x-1
1:(x-1)

x <- seq(1,10)
x
x <- seq(10)
x

x <- seq(from=5, to=10)
x

x <- seq(to=10)
x

x <- seq(1,10,2)
x
x <- seq(10,1,-2)
x

x <- seq(1,10,by=2)
x
x <- seq(from=1,to=10,by=2)  #cannot use 0 for by
x
x <- seq(from=1.0,to=2.0,by=0.2)
x

x <- seq(1,10,length=4)

Repetition

x <- rep(1,times=5)
x
x <- rep('A', times=3)
x

y <- 1:3
x <- rep(y,times=3)
x

x <- rep(1,each=5)
x

x <- rep(c(1,2),times=5)
x

x <- rep(c(1,2),each=5)
x




Comments

Popular posts from this blog

Probability: Binomial Distribution

Probability: Normal Distribution

Hypothesis testing