0% found this document useful (0 votes)
20 views26 pages

Logeshwaran Ex - 2

The document provides an overview of R version 4.4.2, detailing its features, operators, and functionalities. It includes examples of arithmetic, relational, logical, and assignment operations, as well as the creation and usage of functions. Additionally, it highlights variable assignments and demonstrates how to manipulate and print variables in R.
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)
20 views26 pages

Logeshwaran Ex - 2

The document provides an overview of R version 4.4.2, detailing its features, operators, and functionalities. It includes examples of arithmetic, relational, logical, and assignment operations, as well as the creation and usage of functions. Additionally, it highlights variable assignments and demonstrates how to manipulate and print variables in R.
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

OPERATORS:

R version 4.4.2 (2024-10-31 ucrt) -- "Pile of Leaves"

Copyright (C) 2024 The R Foundation for Statistical Computing

Platform: x86_64-w64-mingw32/x64

R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or

'[Link]()' for an HTML browser interface to help.

Type 'q()' to quit R.

[Previously saved workspace restored]

> arithmtic operation:


> v <- c( 4,0.5,8)

> t <- c(5,6,4)

> print(v+t)

[1] 9.0 6.5 12.0

>

>

> v <- c( 9,5.2,7)

> t <- c(1, 2, 4)

> print(v-t)

[1] 8.0 3.2 3.0

>

>

> v <- c(2,4,8)

> t <- c(9,3,6)

> print(v*t)

[1] 18 12 48

>

>

> v <- c( 3,4,7)

> t <- c(9,5,1)

> print(v/t)

[1] 0.3333333 0.8000000 7.0000000

>

>
> v <- c( 6,5.5,6)

> t <- c(8, 3, 4)

> print(v%%t)

[1] 6.0 2.5 2.0

>

>

> v <- c( 2,100,6)

> t <- c(8, 35, 4)

> print(v%/%t)

[1] 0 2 1

>

>

> v <- c( 10,9,8)

> t <- c(6,5,4)

> print(v^t)

[1] 1000000 59049 4096

>

>

> Relational Operators:

Error: unexpected symbol in "Relational Operators"

> v <- c(42,5.5,6,19)

> t <- c(8,2.05,54,69)

> print(v>t)

[1] TRUE TRUE FALSE FALSE


>

>

> v <- c(0.0002,5,65,9)

> t <- c(8,2.5,14,99)

> print(v < t)

[1] TRUE FALSE FALSE TRUE

>

>

> v <- c(0,5.5,16,9)

> t <- c(0,0.0,14,9)

> print(v == t)

[1] TRUE FALSE FALSE TRUE

>

>

> v <- c(6,3.2,6,9)

> t <- c(0,2.5,8,9)

> print(v<=t)

[1] FALSE FALSE TRUE TRUE

>

>

> v <- c(2.3,9.5,6,5)

> t <- c(8,5,1.4,9)

> print(v>=t)

[1] FALSE TRUE TRUE FALSE


>

>

> v <- c(9,59,97,9)

> t <- c(8,5,1,8)

> print(v!=t)

[1] TRUE TRUE TRUE TRUE

>

>

>

> Logical Operators:

Error: unexpected symbol in "Logical Operators"

> v <- c(35,01,TRUE,7+3i)

> t <- c(4,1,FALSE,23+3i)

> print(v&t)

[1] TRUE TRUE FALSE TRUE

>

>

> v <- c(0,0,TRUE,9+6i)

> t <- c(4,0,FALSE,2+3i)

> print(v|t)

[1] TRUE FALSE TRUE TRUE

>

>

> v <- c(9,0,TRUE,7+2i)


> print(!v)

[1] FALSE TRUE FALSE FALSE

>

>

v <- c(3,0,TRUE,2+2i) t <- c(1,3,TRUE,2+3i) print(v&t) [1] TRUE FALSE TRUE TRUE

v <- c(0,0,TRUE,2+2i) t <- c(0,3,TRUE,2+3i) print(v|t) [1] FALSE TRUE TRUE TRUE

> assignment operator:

Error: unexpected symbol in "assignment operator"

> v1 <- c(5,1,TRUE,2+3i)

> v2 <<- c(9,1,TRUE,2+3i)

> v3 = c(6,1,TRUE,2+3i)

> print(v1)

[1] 5+0i 1+0i 1+0i 2+3i

> print(v2)

[1] 9+0i 1+0i 1+0i 2+3i

> print(v3)

[1] 6+0i 1+0i 1+0i 2+3i

>

>

> c(0,1,TRUE,1+3i) -> v1

> c(0,1,TRUE,1+3i) ->> v2

> print(v1)
[1] 0+0i 1+0i 1+0i 1+3i

> print(v2)

[1] 0+0i 1+0i 1+0i 1+3i

>

>

> Miscellaneous Operators:

Error: unexpected symbol in "Miscellaneous Operators"

> v <- 4:16

> print(v)

[1] 4 5 6 7 8 9 10 11 12 13 14 15 16

>

>

> v1 <- 9

> v2 <- 42

> t <- 11:90

> print(v1 %in% t)

[1] FALSE

> print(v2 %in% t)

[1] TRUE

>

>

> M = matrix( c(9,5,1,7,5,5,3), nrow = 2,ncol = 2,byrow = TRUE)

Warning message:

In matrix(c(9, 5, 1, 7, 5, 5, 3), nrow = 2, ncol = 2, byrow = TRUE) :


data length [7] is not a sub-multiple or multiple of the number of rows [2]

> t = M %*% t(M)

> print(t)

[,1] [,2]

[1,] 106 44

[2,] 44 50

>

>

VARIABLES:

Assignment using equal operator.

var.1 = c(5,6,9,7)

Assignment using leftward operator.

var.2 <- c("learn","R")

Assignment using rightward operator.

c(TRUE,1) -> var.3

print(var.1) [1] 5 6 9 7 cat ("var.1 is ", var.1 ,"\n") var.1 is 5 6 9 7 cat ("var.2 is ", var.2 ,"\n")
var.2 is learn R cat ("var.3 is ", var.3 ,"\n") var.3 is 1 1

var_x <- "Welcome" cat("The class of var_x is ",class(var_x),"\n") The class of var_x is
character

var_x <- 34.5 cat(" Now the class of var_x is ",class(var_x),"\n") Now the class of var_x is
numeric
var_x <- 27L cat(" Next the class of var_x becomes ",class(var_x),"\n") Next the class of
var_x becomes integer

print(ls()) [1] "a" "apple" "apple_colors" "BMI" "factor_apple" "myString" "v" "var.1"
"var.2" "var.3" "var_x"

Error: unexpected end of line in ""

List the variables starting with the pattern "var".

print(ls(pattern = "var")) [1] "var.1" "var.2" "var.3" "var_x" print(ls([Link] = TRUE)) [1]
"a" "apple" "apple_colors" "BMI" "factor_apple" "myString" "v" "var.1" "var.2" "var.3"
"var_x"

rm(var.3) print(var.3) Error: object 'var.3' not found

rm(list = ls()) print(ls()) character(0)

R – FUNCTIONS

# Create a sequence of numbers from 32 to 44.

> print(seq(2,94))

[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
63 64 65

[65] 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

>

> # Find mean of numbers from 25 to 82.

> print(mean(25:82))

[1] 53.5

>
> # Find sum of numbers frm 41 to 68.

> print(sum(41:68))

[1] 1526

>

>

> # Create a function to print squares of numbers in sequence.

> [Link] <- function(a) {

+ for(i in 1:a) {

+ b <- i^2

+ print(b)

+ }

+}

>

> # Call the function [Link] supplying 6 as an argument.

> [Link](3)

[1] 1

[1] 4

[1] 9

>

>

> # Create a function without an argument.

> [Link] <- function() {

+ for(i in 1:9) {

+ print(i^2)
+ }

+}

>

> # Call the function without supplying an argument.

> [Link]()

[1] 1

[1] 4

[1] 9

[1] 16

[1] 25

[1] 36

[1] 49

[1] 64

[1] 81

>

>

> [Link] <- function(a,b,c) {

+ result <- a * b + c

+ print(result)

+}

>

> # Call the function by position of arguments.

> [Link](5,10,15)

[1] 65
>

> # Call the function by names of the arguments.

> [Link](a = 5, b = 10, c = 15)

[1] 65

>

>

> # Create a function with arguments.

> [Link] <- function(a = 100, b = 6) {

+ result <- a * b

+ print(result)

+}

>

> # Call the function without giving any argument.

> [Link]()

[1] 600

>

> # Call the function with giving new values of the argument.

> [Link](9,5)

[1] 45

>

>

> # Create a function with arguments.

> [Link] <- function(a, b) {

+ print(a^2)
+ print(a)

+ print(b)

+}

>

> # Evaluate the function without supplying one of the arguments.

> [Link](6)

[1] 36

[1] 6

Error in [Link](6) : argument "b" is missing, with no default

>

>

OPERATORS:

R version 4.4.2 (2024-10-31 ucrt) -- "Pile of Leaves"

Copyright (C) 2024 The R Foundation for Statistical Computing

Platform: x86_64-w64-mingw32/x64

R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale


R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or

'[Link]()' for an HTML browser interface to help.

Type 'q()' to quit R.

[Previously saved workspace restored]

> arithmtic operation:

> v <- c( 4,0.5,8)

> t <- c(5,6,4)

> print(v+t)

[1] 9.0 6.5 12.0

>

>

> v <- c( 9,5.2,7)

> t <- c(1, 2, 4)

> print(v-t)

[1] 8.0 3.2 3.0

>

>
> v <- c(2,4,8)

> t <- c(9,3,6)

> print(v*t)

[1] 18 12 48

>

>

> v <- c( 3,4,7)

> t <- c(9,5,1)

> print(v/t)

[1] 0.3333333 0.8000000 7.0000000

>

>

> v <- c( 6,5.5,6)

> t <- c(8, 3, 4)

> print(v%%t)

[1] 6.0 2.5 2.0

>

>

> v <- c( 2,100,6)

> t <- c(8, 35, 4)

> print(v%/%t)

[1] 0 2 1

>

>
> v <- c( 10,9,8)

> t <- c(6,5,4)

> print(v^t)

[1] 1000000 59049 4096

>

>

> Relational Operators:

Error: unexpected symbol in "Relational Operators"

> v <- c(42,5.5,6,19)

> t <- c(8,2.05,54,69)

> print(v>t)

[1] TRUE TRUE FALSE FALSE

>

>

> v <- c(0.0002,5,65,9)

> t <- c(8,2.5,14,99)

> print(v < t)

[1] TRUE FALSE FALSE TRUE

>

>

> v <- c(0,5.5,16,9)

> t <- c(0,0.0,14,9)

> print(v == t)

[1] TRUE FALSE FALSE TRUE


>

>

> v <- c(6,3.2,6,9)

> t <- c(0,2.5,8,9)

> print(v<=t)

[1] FALSE FALSE TRUE TRUE

>

>

> v <- c(2.3,9.5,6,5)

> t <- c(8,5,1.4,9)

> print(v>=t)

[1] FALSE TRUE TRUE FALSE

>

>

> v <- c(9,59,97,9)

> t <- c(8,5,1,8)

> print(v!=t)

[1] TRUE TRUE TRUE TRUE

>

>

>

> Logical Operators:

Error: unexpected symbol in "Logical Operators"

> v <- c(35,01,TRUE,7+3i)


> t <- c(4,1,FALSE,23+3i)

> print(v&t)

[1] TRUE TRUE FALSE TRUE

>

>

> v <- c(0,0,TRUE,9+6i)

> t <- c(4,0,FALSE,2+3i)

> print(v|t)

[1] TRUE FALSE TRUE TRUE

>

>

> v <- c(9,0,TRUE,7+2i)

> print(!v)

[1] FALSE TRUE FALSE FALSE

>

>

v <- c(3,0,TRUE,2+2i) t <- c(1,3,TRUE,2+3i) print(v&t) [1] TRUE FALSE TRUE TRUE

v <- c(0,0,TRUE,2+2i) t <- c(0,3,TRUE,2+3i) print(v|t) [1] FALSE TRUE TRUE TRUE

> assignment operator:

Error: unexpected symbol in "assignment operator"

> v1 <- c(5,1,TRUE,2+3i)

> v2 <<- c(9,1,TRUE,2+3i)


> v3 = c(6,1,TRUE,2+3i)

> print(v1)

[1] 5+0i 1+0i 1+0i 2+3i

> print(v2)

[1] 9+0i 1+0i 1+0i 2+3i

> print(v3)

[1] 6+0i 1+0i 1+0i 2+3i

>

>

> c(0,1,TRUE,1+3i) -> v1

> c(0,1,TRUE,1+3i) ->> v2

> print(v1)

[1] 0+0i 1+0i 1+0i 1+3i

> print(v2)

[1] 0+0i 1+0i 1+0i 1+3i

>

>

> Miscellaneous Operators:

Error: unexpected symbol in "Miscellaneous Operators"

> v <- 4:16

> print(v)

[1] 4 5 6 7 8 9 10 11 12 13 14 15 16

>

>
> v1 <- 9

> v2 <- 42

> t <- 11:90

> print(v1 %in% t)

[1] FALSE

> print(v2 %in% t)

[1] TRUE

>

>

> M = matrix( c(9,5,1,7,5,5,3), nrow = 2,ncol = 2,byrow = TRUE)

Warning message:

In matrix(c(9, 5, 1, 7, 5, 5, 3), nrow = 2, ncol = 2, byrow = TRUE) :

data length [7] is not a sub-multiple or multiple of the number of rows [2]

> t = M %*% t(M)

> print(t)

[,1] [,2]

[1,] 106 44

[2,] 44 50

>

>

VARIABLES:
Assignment using equal operator.

var.1 = c(5,6,9,7)

Assignment using leftward operator.

var.2 <- c("learn","R")

Assignment using rightward operator.

c(TRUE,1) -> var.3

print(var.1) [1] 5 6 9 7 cat ("var.1 is ", var.1 ,"\n") var.1 is 5 6 9 7 cat ("var.2 is ", var.2 ,"\n")
var.2 is learn R cat ("var.3 is ", var.3 ,"\n") var.3 is 1 1

var_x <- "Welcome" cat("The class of var_x is ",class(var_x),"\n") The class of var_x is
character

var_x <- 34.5 cat(" Now the class of var_x is ",class(var_x),"\n") Now the class of var_x is
numeric

var_x <- 27L cat(" Next the class of var_x becomes ",class(var_x),"\n") Next the class of
var_x becomes integer

print(ls()) [1] "a" "apple" "apple_colors" "BMI" "factor_apple" "myString" "v" "var.1"
"var.2" "var.3" "var_x"

Error: unexpected end of line in ""

List the variables starting with the pattern "var".

print(ls(pattern = "var")) [1] "var.1" "var.2" "var.3" "var_x" print(ls([Link] = TRUE)) [1]
"a" "apple" "apple_colors" "BMI" "factor_apple" "myString" "v" "var.1" "var.2" "var.3"
"var_x"

rm(var.3) print(var.3) Error: object 'var.3' not found

rm(list = ls()) print(ls()) character(0)

R – FUNCTIONS
# Create a sequence of numbers from 32 to 44.

> print(seq(2,94))

[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
63 64 65

[65] 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

>

> # Find mean of numbers from 25 to 82.

> print(mean(25:82))

[1] 53.5

>

> # Find sum of numbers frm 41 to 68.

> print(sum(41:68))

[1] 1526

>

>

> # Create a function to print squares of numbers in sequence.

> [Link] <- function(a) {

+ for(i in 1:a) {

+ b <- i^2

+ print(b)

+ }

+}

>
> # Call the function [Link] supplying 6 as an argument.

> [Link](3)

[1] 1

[1] 4

[1] 9

>

>

> # Create a function without an argument.

> [Link] <- function() {

+ for(i in 1:9) {

+ print(i^2)

+ }

+}

>

> # Call the function without supplying an argument.

> [Link]()

[1] 1

[1] 4

[1] 9

[1] 16

[1] 25

[1] 36

[1] 49

[1] 64
[1] 81

>

>

> [Link] <- function(a,b,c) {

+ result <- a * b + c

+ print(result)

+}

>

> # Call the function by position of arguments.

> [Link](5,10,15)

[1] 65

>

> # Call the function by names of the arguments.

> [Link](a = 5, b = 10, c = 15)

[1] 65

>

>

> # Create a function with arguments.

> [Link] <- function(a = 100, b = 6) {

+ result <- a * b

+ print(result)

+}

>

> # Call the function without giving any argument.


> [Link]()

[1] 600

>

> # Call the function with giving new values of the argument.

> [Link](9,5)

[1] 45

>

>

> # Create a function with arguments.

> [Link] <- function(a, b) {

+ print(a^2)

+ print(a)

+ print(b)

+}

>

> # Evaluate the function without supplying one of the arguments.

> [Link](6)

[1] 36

[1] 6

Error in [Link](6) : argument "b" is missing, with no default

>

>

You might also like