R Language.

#R Language 

#to run the code go here

https://www.mycompiler.io/new/r

#my first programming in R environment

mystring <- "hello friend"

print(mystring)

#since R lang does not support multiline comment but there is a trick

if (FALSE){

    "so as you know r does not support multi comment but this is trick"

}


myString <- "hello I am faizan"

print(myString)

#create the data frame


emp.data <- data.frame(

  

  emp_id = c (1:4),

  

  emp_name = c("faizan","hammad","salman","maroof"),

  

  salary = c(23.4,34.5,44.5,55.6),

  

  

  

  start_date = as.Date(c("2012-01-01", "2013-09-23", "2013-04-23", "2013-04-24")),

  

  stringAsFactor = FALSE

  

)


#print the data frame


print(emp.data)


#get the structure of the data frame


str(emp.data)


#print the summary


print(summary(emp.data))


#Extract Specific columns


result <- data.frame(emp.data$emp_name,emp.data$salary)


print(result)


#extract first two rows


result <- emp.data[1:2,]


print(result)


#extract 3rd and 5th row with 2nd and 4th columns


result <- emp.data[c(2,4),c(1,3)]


print(result)


#add the extra 'dept' columns


emp.data$dept <- c("IT","OPERATORS","HR","FINANCE")


v <- emp.data


print(v)


#now create the second data frame


emp.newdata <- data.frame(

  

  emp_id = c(5:7),

  

  emp_name = c("wahid","sami","tausif"),

  

  salary = c(23.3,55.4,54.6),

  

  start_date = as.Date(c("2013-04-15","2013-04-23","2013-12-17")),

  

  dept = c("IT","OPERATOR","FINANCE"),

  

  stringAsFactor = FALSE

  

)

#bind the two data frames


emp.finaldata <- rbind(emp.data,emp.newdata) 

print(emp.finaldata)


#R-data types


#creating a vector

apple <-c('red','green','yellow')

print(apple)

#get class of the vector

print(class(apple))


#create a list

list1 <- list(c(2,3,4),21.3,23.4,sin,cos)

print(list1)


#create a matrix

M = matrix(c('a','b','c','d'), ncol = 3, byrow = TRUE)

print(M)


#create a arrays

a <- array(c('green','yellow'),dim = c(3,3,2))

print(a)


#create a factor

apple_color <- c('green','yellow','red','green','red','black')

#create a factor object

factor_apple <- factor(apple_color)



#print the factor

print(factor_apple)

print(nlevels(factor_apple))


#create the data frames

body_mass_index <- data.frame(

    gender = c("faizan","salman","hammad"),

    height = c(182, 186, 140),

    weight = c(81, 78, 60),

    age = c(19, 18, 19)

)

print(body_mass_index)


#Elements are arranged sequentially by row.

M <- matrix(c(3:14), nrow = 4, byrow = TRUE)

print(M)


#Elements are arranged sequentially by column

N <- matrix(c(3:14), nrow = 4,  byrow = FALSE)

print(N)


#Define the column and row names.

rownames = c("row1","row2","row3","row4")

colnames = c("col1","col2","col3")


p <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))

print(p)


#Accessing Elements of a matrix


# Define the column and row names.

rownames = c("row1", "row2", "row3", "row4")

colnames = c("col1", "col2", "col3")


# Create the matrix.

P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))


# Access the element at 3rd column and 1st row.

print(P[1,3])


# Access the element at 2nd column and 4th row.

print(P[4,2])


# Access only the 2nd row.

print(P[2,])


# Access only the 3rd column.

print(P[,3])


Matrix Addition & Subtraction


# Create two 2x3 matrices.

matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)

print(matrix1)

matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)

print(matrix2)

# Add the matrices.

result <- matrix1 + matrix2

cat("Result of addition","\n")

print(result)

# Subtract the matrices

result <- matrix1 - matrix2

cat("Result of subtraction","\n")

print(result)


#Matrix Multiplication & Division


# Create two 2x3 matrices.
matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
print(matrix1)
matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
print(matrix2)

# Multiply the matrices.
result <- matrix1 * matrix2
cat("Result of multiplication","\n")
print(result)

# Divide the matrices
result <- matrix1 / matrix2
cat("Result of division","\n")
print(result)

#it is directly copied from book okie dude.

Comments

Post a Comment