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

R Lab

The document shows examples of using built-in functions in R for mathematical operations, statistics, strings, creating matrices and data frames. Functions demonstrated include summing vectors, calculating factorials, mean, median, concatenating strings, and creating a data frame with columns for name, roll number, class, and age. Examples are provided of creating matrices by row and column and viewing their structure.
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)
53 views

R Lab

The document shows examples of using built-in functions in R for mathematical operations, statistics, strings, creating matrices and data frames. Functions demonstrated include summing vectors, calculating factorials, mean, median, concatenating strings, and creating a data frame with columns for name, roll number, class, and age. Examples are provided of creating matrices by row and column and viewing their structure.
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

Adding Two Vectors

> a <- c(1,2,3,4,5)


> b <- c(1,2,3,4,5)
> c <- a+b
> print(c)

[1] 2 4 6 8 10
Factorial of the Given Number

> factorial<- function(num)


+{
+ if(num<=1)
+ {
+ return(1)
+ }
+ else
+ {
+ return(num*factorial(num-1))
+ }
+}

> begin <- function(num)


+{
+ num =as.integer(readline(prompt = "Enter a number :"))
+ factorial = 1
+ if(num<0)
+ {
+ print("Factorial doesn't exist for negative numbers")
+ }
+ else if(num==0)
+ {
+ print("Factorial of 0 is 1")
+ }
+ else
+ {
+ print(paste("Factorial of ",num," is ",factorial(num)))
+ }
+}

> begin()
Enter a number : 5
[1] "Factorial of 5 is 120"

> begin()
Enter a number : -8
[1] "Factorial doesn't exist for negative numbers"

> begin()
Enter a number : 0
[1] "Factorial of 0 is 1"
Built In Functions

> # MATHEMATICAL FUNCTIONS

> # sum
> print(paste("Sum = ",sum(41:46)))
[1] "Sum = 261"

> # square root


> print(paste("Square root of 12 = " ,sqrt(12)))
[1] "Square root of 12 = 3.46410161513775"

> # power
> print(paste("4 to the power of 3 = ",4**3))
[1] "4 to the power of 3 = 64"

> # floor
> print(paste("Floor of 3.471 = ",floor(3.471)))
[1] "Floor of 3.471 = 3"

> # ceiling
> print(paste("Ceiling of 3.471 = ",ceiling(3.471)))
[1] "Ceiling of 3.471 = 4"

> # truncate
> print(paste("Truncate of 3.471 = " ,trunc(3.471)))
[1] "Truncate of 3.471 = 3"

> # exponent
> print(paste("Exponent of 3.471 = ",exp(3.471)))
[1] "Exponent of 3.471 = 32.1688952609266"

> # STATISTICAL FUNCTIONS

> # mean
> print(paste("Mean = ",mean(25:35)))
[1] "Mean = 30"

> # median
> print(paste("Medina = ",median(25:35)))
[1] "Medina = 30"
> # mode
> print(paste("Mode = ",mode(25:35)))
[1] "Mode = numeric"

> # minimum
> print(paste("Minimum = ",min(25:35)))
[1] "Minimum = 25"

> # maximum
> print(paste("Maximum = ",max(25:35)))
[1] "Maximum = 35"
String Functions

> a <- "HELLO Chinthore"


> b <- "Ravichandran"

> # concatenation
> print(paste(a,b,sep = " "))
[1] "HELLO Chinthore Ravichandran"

> # right justify


> print(format(a,justify = "r",width = 35))
[1] " HELLO Chinthore"

> # left justify


> print(format(a,justify = "l",width = 35))
[1] "HELLO Chinthore "

> # center justify


> print(format(a,justify = "c",width = 35))
[1] " HELLO Chinthore "
> # tolower
> print(tolower(a))
[1] "hello chinthore"

> # to upper
> print(toupper(b))
[1] "RAVICHANDRAN"

> # extract
> print(substring(a,6,10))
[1] "Chin"

> # length
> print(length(a))
[1] 1

> # split
> print(strsplit(a,""))
[[1]]
[1] "H" "E" "L" "L" "O" " " "C" "h" "i" "n" "t" "h" "o" "r" "e"
Data Frame

> a <- ("Name" = c("Ana","Mike","Paul"))


> b <- ("Roll No" = c("2,5,7"))
> c <- ("Class" = c("Alpha,Beta,Gamma"))
> d <- ("Age" = c(12,34,45))
> x <- data.frame(a,b,c,d)

> str(x)
'data.frame': 3 obs. of 4 variables:
$ a: Factor w/ 3 levels "Ana","Mike","Paul": 1 2 3
$ b: Factor w/ 1 level "2,5,7": 1 1 1
$ c: Factor w/ 1 level "Alpha,Beta,Gamma": 1 1 1
$ d: num 12 34 45

> typeof(x)
[1] "list"

> typeof(a)
[1] "character"

> typeof(b)
[1] "character"

> typeof(c)
[1] "character"

> typeof(d)
[1] "double"

> class(x)
[1] "data.frame"

> names(x)
[1] "a" "b" "c" "d"

> nrow(x)
[1] 3

> length(x)
[1] 4
> x["a"]
a
1 Ana
2 Mike
3 Paul

> x["b"]
b
1 2,5,7
2 2,5,7
3 2,5,7

> x["c"]
c
1 Alpha,Beta,Gamma
2 Alpha,Beta,Gamma
3 Alpha,Beta,Gamma

> x["d"]
d
1 12
2 34
3 45
Matrix Creation

> a <- matrix(c(1:9), nrow = 3, byrow = TRUE)


> print(a)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

> b <- matrix(c(1:9), nrow = 3, byrow = FALSE)


> print(b)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

> c <- matrix(c(1:9), ncol = 3)


> print(c)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

You might also like