Posted on

# Assigning value and printing value

a = 1 #using =
print(a) #printing a
b <- a+2 #using <-
print(b)
4 -> a #using ->
print(a)

# printing multiple data types

cat("value of a: ", a, "value of b: ", b)

print(class(a))

print(ls())

# so use this command to show them too

print(ls(all.names = TRUE))

# print variable at workspace starting with 'as'

print(ls(pattern = 'as'))

rm(a)

rm(list = ls())

# class gives type of variable

B=4
print(class(B)) # B is a numeric
C= 5L
print(paste('The type of C is',class(C))) # C is integer

# adding to vectors mean array

v <- c(2, 0, 4, 1)
d <- c(2, 14, 0, 11)
sum2 <- (v+d)
print(sum2)

sum4 <- sum(v,d)
print(sum4)

sum5 <- sum(v)
print(sum5)

print(d-v)

print(d*v)

print(d/v)

print(d > v)
print(d < v)
print(d == v)
print(d >= v)
print(d <= v)
print(d != v)

print(d & v)
print(d | v)
print(!v)

a <- 3
a = 3
a <<- 3

3 -> a
3 ->> a

d <- 1:8
a <- 4
b <- a %in% d
print(b)

# Multiply matrix with its transpose

ma = matrix( c(1,2,3,4,5,6), nrow = 2,ncol = 3,byrow = TRUE) #making matrix
tra = ma %*% t(ma)
print(tra)

# to check a is integer

a <- 10L #L for integer data, integer is also numeric but numeric is not integer
is.integer(a) #true if a is integer
is.numeric(a) #true if a is numeric
is.character(a) #true if a is character

# if else condition

if(a<2){
print("a is less than 2")
}else{
print("a is greater")
}

# array

arr<-c(1,2,3,4,5)
print(arr)

arr<-1:4

arr<-4:1

# loop and continue

s<-"value in v"
v<-4:1 #v=4,3,2,1 array
for(c in v){ #c = 4,3,2,1 values from 4 to 1
#print(s)
if(c==2){
next #skipt 2
}
print (c)
}

# repeat and break mean do-while

r<-1
repeat{ #repeat
print(r)
if(r==6){ #until condition match
break

}
r<-r+1
}

# while

w<-1 #starting value
while(w<3){ #condition
print(w)
w<-w+1 #incrementing
}

a<-1
b<-2
c<-3
d<-a+b+c
return(d)
}
print(d)

# convert string or character into integer and store into b

b<- as.integer(a)

# creat list of diff data types and store in list1

list1 <- list(c(11,12,13,14), "m_Sam", sin, 2)
print(list1[[1]]) #printin first index data that is array
print(list1[[1]][2]) #printing the 2nd element of array

# fibonacci Series

arr[1]<-1
arr[2]<-1
fibonacci <- function(n){
print(arr[1])
print(arr[2])
for (v in 3:n){
arr[v]<-arr[v-1]+arr[v-2]
print(arr[v])
}
}
fibonacci(10)

# creat matrix

ma <- matrix(c(2,3,4,5,6,7), nrow = 2,ncol = 3, byrow = TRUE)
print(ma)

# factors create levels base of distinct values

arr<-c('red', 'green', 'blue', '1', '1', 'red')
fac<-factor(arr)
print(fac)
print(nlevels(fac)) #LEVELS

# data frame

fra <- data.frame(
name = c('zahid ali', 'abdul samad', 'waleed', 'm_No1'),
age = c(20, 19, 19, 25),
gender = c('male', 'male', 'male', 'female')
)
print(fra)

x <- switch(
2, #expression
'Abdul', #case1
'Bhatti' #case3
)
print(x)

# input from user in array using for loop

rm(list = ls())
arr<-
v <- 1:5
for (i in v) {
arr[i] <- readline(prompt = "enter value: ")
}

f<-c(4,0,3,1,2)
sum_G<<-sum(f)
print(sum_G)

# printing multiple strings

a<-"Ab"
b<-"Sam"
c<-"Dev"
print(paste(c,a,b,sep="-", collapse = ""))

# Total number of digits displayed. Last digit rounded off.

result <- format(23.123456789, digits = 9)
print(result)

# Display numbers in scientific notation.

result <- format(c(6, 13.14521), scientific = TRUE)
print(result)

# The minimum number of digits to the right of the decimal point.

result <- format(23.47, nsmall = 5)
print(result)

# Format treats everything as a string.

result <- format(6)
print(result)

# Numbers are padded with blank in the beginning for width.

result <- format(13.7, width = 6)
print(result)

# Left justify strings.

result <- format("Hello", width = 8, justify = "1")
print(result)

# Justfy string with center.

result <- format("Hello", width = 8, justify = "c")
print(result)

# factorial

factn<-function(n){
f<-1
for(i in 1:n){
f=f*i
}
print (f)
}
n=0
factn(n)