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

Class 2

The document demonstrates various date/time and numeric functions in R including date(), Sys.Date(), format(), floor(), ceiling(), round(), and vector/matrix operations like rep(), cumsum(), and matrix(). It also shows how to create, name and subset matrices and vectors. Various functions for character vectors and strings are presented such as nchar(), paste() and collapse.

Uploaded by

Roli Dube
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Class 2

The document demonstrates various date/time and numeric functions in R including date(), Sys.Date(), format(), floor(), ceiling(), round(), and vector/matrix operations like rep(), cumsum(), and matrix(). It also shows how to create, name and subset matrices and vectors. Various functions for character vectors and strings are presented such as nchar(), paste() and collapse.

Uploaded by

Roli Dube
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

> date()

[1] "Wed Sep 02 14:11:35 2020"

> DATE()

Error in DATE() : could not find function "DATE"

> dt1= date()

> dat1

Error: object 'dat1' not found

> dt1

[1] "Wed Sep 02 14:12:32 2020"

> dt1+90

Error in dt1 + 90 : non-numeric argument to binary operator

> Sys.Date()

[1] "2020-09-02"

> sys.date()

Error in sys.date() : could not find function "sys.date"

> Sys.time()

[1] "2020-09-02 14:15:39 IST"

>

> dt2 = Sys.Date()

> dt2

[1] "2020-09-02"

> format (dt2, format = "%b %d %Y")

[1] "Sep 02 2020"

> format (dt2, format = "%b %d, %Y")

[1] "Sep 02, 2020"

Types of Vectors
> a=5

> b=2
> a/b

[1] 2.5

> a^b

[1] 25

> a**b

[1] 25

> a %% b

[1] 1

> b %% a

[1] 2

> a %/% b

[1] 2

log (1)

[1] 0

> n = -1:10

>n

[1] -1 0 1 2 3 4 5 6 7 8 9 10

> n= -(1:10)

>n

[1] -1 -2 -3 -4 -5 -6 -7 -8 -9 -10

> n=6

> r=3

> choose(n,r)

[1] 20

> n=123.456789

> x= -123.456789

> floor()

Error in floor() : 0 arguments passed to 'floor' which requires 1


> floor(n)

[1] 123

> floor(x)

[1] -124

> ceiling(n)

[1] 124

> trunc (n)

[1] 123

> round(n)

[1] 123

> round (n,2)

[1] 123.46

> round (n,0)

[1] 123

> round (n,1)

[1] 123.5

> round (n,-1)

[1] 120

> round (n,-2)

[1] 100

VECTOR OPERATOR

> '+' (2,3)

[1] 5

> '-' (2,3)

[1] -1

> '*' (2,3)

[1] 6

> '^' (2,3)


[1] 8

> '/' (2,3)

[1] 0.6666667

> '%' (2,3)

Error in `%`(2, 3) : could not find function "%"

> '%/%' (2,3)

[1] 0

> '%%' (2,3)

[1] 2

> '**' (2,3)

Error in `**`(2, 3) : could not find function "**"

y = seq(from = 1, to = 100, by = 2)

>y

[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45

[24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91

[47] 93 95 97 99

> y = seq(1,100,2)

>y

[1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45

[24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91

[47] 93 95 97 99

> seq (1, 100, length.out = 25)

[1] 1.000 5.125 9.250 13.375 17.500 21.625 25.750 29.875

[9] 34.000 38.125 42.250 46.375 50.500 54.625 58.750 62.875

[17] 67.000 71.125 75.250 79.375 83.500 87.625 91.750 95.875

[25] 100.000

> i = c(3,5,7,9)
> letters [i]

[1] "c" "e" "g" "i" HAVE TO MAKE A CONSTRUCT FOR THIS TO WORK

>

> letters [-21]

[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p"

[17] "q" "r" "s" "t" "v" "w" "x" "y" "z"

> letters [c(3,5,7,9)]

[1] "c" "e" "g" "i"

> a = c(11,13,21)

> b= c(9,26,16)

> which (a>b)

[1] 1 3

> d = which (a>b)

>d

[1] 1 3

> a[which (a>b)]

[1] 11 21

> x = rep(1,10)

>x

[1] 1 1 1 1 1 1 1 1 1 1

> which (x==1)

[1] 1 2 3 4 5 6 7 8 9 10

> any (x==1)

[1] TRUE

> all (x==1)


[1] TRUE

> any (x!=1)

[1] FALSE

> a = c(11,13,21)

> cumsum(a)

[1] 11 24 45

> cumprod(a)

[1] 11 143 3003

> cummmax(a)

Error in cummmax(a) : could not find function "cummmax"

> cummax(a)

[1] 11 13 21

> cummin(a)

[1] 11 11 11

> v= c(17,19,26,11,5,2)

> cummax(v)

[1] 17 19 26 26 26 26

> cummin(v)

[1] 17 17 17 11 5 2

> diff(v)

[1] 2 7 -15 -6 -3

> sum (v)

[1] 80

>

> matrix (0,5,2)

[,1] [,2]

[1,] 0 0
[2,] 0 0

[3,] 0 0

[4,] 0 0

[5,] 0 0

> x= matrix(letters, 13,2)

>x

[,1] [,2]

[1,] "a" "n"

[2,] "b" "o"

[3,] "c" "p"

[4,] "d" "q"

[5,] "e" "r"

[6,] "f" "s"

[7,] "g" "t"

[8,] "h" "u"

[9,] "i" "v"

[10,] "j" "w"

[11,] "k" "x"

[12,] "l" "y"

[13,] "m" "z"

> matrix (*,5,2)

Error: unexpected '*' in "matrix (*"

> matrix ("*",5,2)

[,1] [,2]

[1,] "*" "*"

[2,] "*" "*"

[3,] "*" "*"

[4,] "*" "*"

[5,] "*" "*"


> x= matrix(letters, 10,3)

Warning message:

In matrix(letters, 10, 3) :

data length [26] is not a sub-multiple or multiple of the number of rows [10]

> matrix (letters,14,3)

[,1] [,2] [,3]

[1,] "a" "o" "c"

[2,] "b" "p" "d"

[3,] "c" "q" "e"

[4,] "d" "r" "f"

[5,] "e" "s" "g"

[6,] "f" "t" "h"

[7,] "g" "u" "i"

[8,] "h" "v" "j"

[9,] "i" "w" "k"

[10,] "j" "x" "l"

[11,] "k" "y" "m"

[12,] "l" "z" "n"

[13,] "m" "a" "o"

[14,] "n" "b" "p"

Warning message:

In matrix(letters, 14, 3) :

data length [26] is not a sub-multiple or multiple of the number of rows [14]

> matrix (letters,13,2)

[,1] [,2]

[1,] "a" "n"

[2,] "b" "o"

[3,] "c" "p"

[4,] "d" "q"


[5,] "e" "r"

[6,] "f" "s"

[7,] "g" "t"

[8,] "h" "u"

[9,] "i" "v"

[10,] "j" "w"

[11,] "k" "x"

[12,] "l" "y"

[13,] "m" "z"

> matrix (letters,13,2, byrow = TRUE)

[,1] [,2]

[1,] "a" "b" l

[2,] "c" "d"

[3,] "e" "f"

[4,] "g" "h"

[5,] "i" "j"

[6,] "k" "l"

[7,] "m" "n"

[8,] "o" "p"

[9,] "q" "r"

[10,] "s" "t"

[11,] "u" "v"

[12,] "w" "x"

[13,] "y" "z"

> d= matrix(5,2,2)

>d

[,1] [,2]

[1,] 5 5
[2,] 5 5

> d [1,1]

[1] 5

> d [2,1]

[1] 5

> cnm = c("Team A", "Team b")

> rnm = c("game 1", "gaame 2")

> colnames (d)= cnm

> rownames (d)= rnm

>d

Team A Team b

game 1 5 5

gaame 2 5 5

> nm= "Adam"

> nm

[1] "Adam"

> length(nm)

[1] 1

> char [nm]

Error: object 'char' not found

> nchar (nm)

[1] 4

> nm2= "Smith"

> eco = c(nm,nm2)

> eco

[1] "Adam" "Smith"

> length (eco)

[1] 2
> nchar (eco)

[1] 4 5

> b = paste(letters, LETTERS, collapse = " ")

>b

[1] "a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s S t T u U v V w W x X y Y z Z"

> length (b)

[1] 1

> b = paste(letters, LETTERS, collapse = "")

>b

[1] "a Ab Bc Cd De Ef Fg Gh Hi Ij Jk Kl Lm Mn No Op Pq Qr Rs St Tu Uv Vw Wx Xy Yz Z"

> b = paste(letters, LETTERS, collapse =" ")

>b

[1] "a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Q r R s S t T u U v V w W x X y Y z Z"

>

You might also like