R Examples
Example fo Basic R :
In this post we discuss R Examples
Variable Declaration:
variable1 <- 14
(OR)
variable1 = 14
Remove variable:
rm(variable1)
List the variables:
ls()
Remove All Variables:
rm(list=ls())
Arithmetic Operator:
> a=10
> b=15
> a+b
> 25
Relational Operator:
> a=16
> b=27
> a > b
> FALSE
Logical Operator:
> a = TRUE > b = FALSE
> a|b
> TRUE
> a&b
> FALSE
Miscellaneous Operators:
> n=2:5
[1] 2 3 4
IF statement:
> a <- “Geoinsyssoft”
> if (is.character(a)) {
> print(“a is character”)
> }
[1] “a is character”
Switch statement:
> a <- switch(2, “one”,”two”,”three”,”four”)
> print(a)
[1] “two”
While Statement:
> a <- c(“HI”, “This is Geoinsyssoft”)
> n <- 6
> while(n>=0) {
> print(a)
> n=n-1
> }
[1] “HI” “This is Geoinsyssoft”
[1] “HI” “This is Geoinsyssoft”
[1] “HI” “This is Geoinsyssoft”
[1] “HI” “This is Geoinsyssoft”
[1] “HI” “This is Geoinsyssoft”
[1] “HI” “This is Geoinsyssoft”
[1] “HI” “This is Geoinsyssoft”
FOR statement:
> v <- 1:6
> for (i in v ) {
> print(“Welcome to Geoinsysoft”)
> }
[1] “Welcome to Geoinsysoft”
[1] “Welcome to Geoinsysoft”
[1] “Welcome to Geoinsysoft”
[1] “Welcome to Geoinsysoft”
[1] “Welcome to Geoinsysoft”
[1] “Welcome to Geoinsysoft”
Built-in Function:
Sequence Function:
> print(seq(32,44))
[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
Mean:
> print(mean(25:82))
[1] 53.5
User-defined Function:
> c <-2
> new.function <- function(a) {
> for(i in 1:a) {
> b <- i^(2*c)
> print(b)
> }
> }
> new.function(2)
[1] 1
[1] 16
Mean:
> a <- c(10,18,30,29,340,29)
> mean(a)
[1] 76
Median:
> sort(a)
[1] 10 18 29 29 30 340
> median(sort(a))
[1] 29
List:
> list_data <- list(“Red”, “Green”, c(1,2,3), TRUE, 5, 9.2)
> list_data
[[1]]
[1] “Red”
[[2]]
[1] “Green”
[[3]]
[1] 1 2 3
[[4]]
[1] TRUE
[[5]]
[1] 5
[[6]]
[1] 9.2
Matrics:
Create Matrix
> a <- matrix(c(1:12), nrow = 4, byrow = TRUE)
> a
[,1] [,2][,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
Give name to the row and column
> dimnames(a)=list(c(“row1″,”row2″,”row3″,”row4”),c(“col1″,”col2″,”col3”))
> a
col1 col2 col3
row1 1 5 9
row2 2 6 10
row3 3 7 11
row4 4 8 12
Select Particular value
> print(a[4,3])
[1] 12
Matrix Multiplication
> a <- matrix(c(1:12), nrow = 4, byrow = TRUE)
> b <- matrix(c(5:16), nrow = 4, byrow = TRUE)
> c <- a*b
> c
[,1] [,2] [,3]
[1,] 5 12 21
[2,] 32 45 60
[3,] 77 96 117
[4,] 140 165 192
Array:
Creating Array
> a<-c(10,20,30,40)
> b<-c(20,30,40,50,60,70,80,90)
> c<-array(c(a,b),dim=c(5,5,3))
> c
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 10 30 80 40 60
[2,] 20 40 90 20 70
[3,] 30 50 10 30 80
[4,] 40 60 20 40 90
[5,] 20 70 30 50 10
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 20 40 90 20 70
[2,] 30 50 10 30 80
[3,] 40 60 20 40 90
[4,] 20 70 30 50 10
[5,] 30 80 40 60 20
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 30 50 10 30 80
[2,] 40 60 20 40 90
[3,] 20 70 30 50 10
[4,] 30 80 40 60 20
[5,] 40 90 20 70 30
Factor:
Create Factor
> b<-factor(a)
> b
[1] 10 20 30 40
Levels: 10 20 30 40
Factor Using Data Frame
> Height <- c(132,151,162,139,166,147,122)
> Weight <- c(48,49,66,53,67,52,40)
> Name <- c(“aaa”,”bbb”,”ccc”,”ddd”,”eee”,”fff”,”ggg”)
> a <- data.frame(Name,Height,Weight)
> a
Name Height Weight
1 aaa 132 48
2 bbb 151 49
3 ccc 162 66
4 ddd 139 53
5 eee 166 67
6 fff 147 52
7 ggg 122 40
> print(is.factor(a$Name))
[1] TRUE
> print((a$Name))
[1] aaa bbb ccc ddd eee fff ggg
Levels: aaa bbb ccc ddd eee fff ggg