R Lab
R Lab
[1] 2 4 6 8 10
Factorial of the Given Number
> 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
> # sum
> print(paste("Sum = ",sum(41:46)))
[1] "Sum = 261"
> # 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"
> # 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
> # concatenation
> print(paste(a,b,sep = " "))
[1] "HELLO Chinthore Ravichandran"
> # 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
> 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