0% found this document useful (0 votes)
127 views12 pages

R Programming

The document demonstrates various R functions and operations including: 1) Creating vectors and assigning values, performing basic arithmetic operations on vectors, and changing dimensions of vectors using dim(); 2) Creating matrices from vectors using cbind() and changing their dimensions; 3) Taking square roots and cubes of values in a vector, binding the results together, and changing it to a matrix; 4) Checking class of objects, coercing between classes, and examining structure using str().

Uploaded by

Roli Dube
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views12 pages

R Programming

The document demonstrates various R functions and operations including: 1) Creating vectors and assigning values, performing basic arithmetic operations on vectors, and changing dimensions of vectors using dim(); 2) Creating matrices from vectors using cbind() and changing their dimensions; 3) Taking square roots and cubes of values in a vector, binding the results together, and changing it to a matrix; 4) Checking class of objects, coercing between classes, and examining structure using str().

Uploaded by

Roli Dube
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

> a <- "hello world"

>a

[1] "hello world"

> print ("hello world")

[1] "hello world"

> z = 1:5

>z

[1] 1 2 3 4 5

> print (z)

[1] 1 2 3 4 5

> paste (z)

[1] "1" "2" "3" "4" "5"

> m = paste(z)

>m

[1] "1" "2" "3" "4" "5"

> z = paste(z)

>z

[1] "1" "2" "3" "4" "5"

2.

> n = 1:10

>n

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

> k = n+2

>k

[1] 3 4 5 6 7 8 9 10 11 12

> n+k

[1] 4 6 8 10 12 14 16 18 20 22
> c= 12:21

>c

[1] 12 13 14 15 16 17 18 19 20 21

> n+c

[1] 13 15 17 19 21 23 25 27 29 31

>c

[1] 12 13 14 15 16 17 18 19 20 21

> h=30:33

>h

[1] 30 31 32 33

> h+c

[1] 42 44 46 48 46 48 50 52 50 52

Warning message:

In h + c : longer object length is not a multiple of shorter object length

> sum(1;10)

Error: unexpected ';' in "sum(1;"

> sum (1:10)

[1] 55

> n = c(1:5)

>n

[1] 1 2 3 4 5

> p = c(50:51)

>p

[1] 50 51

> p = c(50:100)

>p

[1] 50 51 52 53 54 55 56 57 58 59 60 61 62

[14] 63 64 65 66 67 68 69 70 71 72 73 74 75
[27] 76 77 78 79 80 81 82 83 84 85 86 87 88

[40] 89 90 91 92 93 94 95 96 97 98 99 100

> p[6]

[1] 55

>

Q.

> k=c(1:10)

>k

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

> s= k^2

>s

[1] 1 4 9 16 25 36 49 64 81 100

> m= k^3

>m

[1] 1 8 27 64 125 216 343 512 729 1000

> print(k,s,m)

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

> paste(k,s,m)

[1] "1 1 1" "2 4 8" "3 9 27"

[4] "4 16 64" "5 25 125" "6 36 216"

[7] "7 49 343" "8 64 512" "9 81 729"

[10] "10 100 1000"

>b = c(k,s,m)

>b

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

[11] 1 4 9 16 25 36 49 64 81 100

[21] 1 8 27 64 125 216 343 512 729 1000


> p=c(10:15)

> q = c("great", "people")

>q

[1] "great" "people"

>p

[1] 10 11 12 13 14 15

> paste (p,q)

[1] "10 great" "11 people" "12 great" "13 people"

[5] "14 great" "15 people"

> c(p,q)

[1] "10" "11" "12" "13" "14" "15"

[7] "great" "people"

> print(p,q)

Error in print.default(p, q) : invalid printing digits -2147483648

In addition: Warning message:

In print.default(p, q) : NAs introduced by coercion

Class 2:

Default length of any numeric expression is 7

> 11/7

[1] 1.571429

> 110/7

[1] 15.71429

> f= 11/7

>f

[1] 1.571429

> print(f, digit=4)

[1] 1.571

> print(f, digit=2)


[1] 1.6

> print(f, digit=1) rounds off the value

[1] 2

> print (f, digit=-1)

Error in print.default(f, digit = -1) : invalid printing digits -1

> print (f, digit=0)

[1] 2

>> getwd()

[1] "C:/Users/user/Documents"

> setwd("C:/Users/user/Documents/R files")

Error in setwd("C:/Users/user/Documents/R files") :

cannot change working directory

> setwd("C:/Users/user/Documents\R files")

Error: '\R' is an unrecognized escape in character string starting ""C:/Users/user/Documents\R"

> setwd("C:/Users/user/Documents\R files")

> x= (1:12)

>x

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

> dim (x)= c(2,6)

>x

[,1] [,2] [,3] [,4] [,5] [,6]

[1,] 1 3 5 7 9 11

[2,] 2 4 6 8 10 12

> dim (x)= c(3,4)

>x

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

[1,] 1 4 7 10

[2,] 2 5 8 11
[3,] 3 6 9 12

> dim (x)= c(6,2)

>x

[,1] [,2]

[1,] 1 7

[2,] 2 8

[3,] 3 9

[4,] 4 10

[5,] 5 11

[6,] 6 12

> t(x)

[,1] [,2] [,3] [,4] [,5] [,6]

[1,] 1 2 3 4 5 6

[2,] 7 8 9 10 11 12

Q. 1st 10 natural nos in a column

> k =(1:10)

>k

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

> y = k^2

> z= k^3

>y

[1] 1 4 9 16 25 36 49 64 81 100

>z

[1] 1 8 27 64 125 216 343 512 729 1000

> s = c(k,y,z)

>s

[1] 1 2 3 4 5 6 7 8 9 10
[11] 1 4 9 16 25 36 49 64 81 100

[21] 1 8 27 64 125 216 343 512 729 1000

> dim (s)= c(10,3)

>s

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

[1,] 1 1 1

[2,] 2 4 8

[3,] 3 9 27

[4,] 4 16 64

[5,] 5 25 125

[6,] 6 36 216

[7,] 7 49 343

[8,] 8 64 512

[9,] 9 81 729

[10,] 10 100 1000

>c(1:10)

> b=a^2

> c=a^3

> a=c(a,b,c)

>a

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

[11] 1 4 9 16 25 36 49 64 81 100

[21] 1 8 27 64 125 216 343 512 729 1000

>

> a= cbind(a,b,c)

>a

a b c

[1,] 1 1 1
[2,] 2 4 8

[3,] 3 9 27

[4,] 4 16 64

[5,] 5 25 125

[6,] 6 36 216

[7,] 7 49 343

[8,] 8 64 512

[9,] 9 81 729

[10,] 10 100 1000

[11,] 1 1 1

[12,] 4 4 8

[13,] 9 9 27

[14,] 16 16 64

[15,] 25 25 125

[16,] 36 36 216

[17,] 49 49 343

[18,] 64 64 512

[19,] 81 81 729

[20,] 100 100 1000

[21,] 1 1 1

[22,] 8 4 8

[23,] 27 9 27

[24,] 64 16 64

[25,] 125 25 125

[26,] 216 36 216

[27,] 343 49 343

[28,] 512 64 512

[29,] 729 81 729

[30,] 1000 100 1000


> k=(a,b,c)

Error: unexpected ',' in "k=(a,"

> k=c(a,b,c)

>k

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

[11] 1 4 9 16 25 36 49 64 81 100

[21] 1 8 27 64 125 216 343 512 729 1000

[31] 1 4 9 16 25 36 49 64 81 100

[41] 1 4 9 16 25 36 49 64 81 100

[51] 1 4 9 16 25 36 49 64 81 100

[61] 1 8 27 64 125 216 343 512 729 1000

[71] 1 8 27 64 125 216 343 512 729 1000

[81] 1 8 27 64 125 216 343 512 729 1000

[91] 1 4 9 16 25 36 49 64 81 100

[101] 1 8 27 64 125 216 343 512 729 1000

> dim(k)=c(10,3)

Error in dim(k) = c(10, 3) :

dims [product 30] do not match the length of object [110]

> dim(k)= c(10,3) check with someone

Error in dim(k) = c(10, 3) :

dims [product 30] do not match the length of object [110]

> x=45

> mode(x)

[1] "numeric"

> c="INDIA"

> mode(c)

[1] "character"

> q= TRUE
> mode (q)

[1] "logical"

> k = paste(x,c,q)

>k

[1] "45 INDIA TRUE"

> j= c(x,c,q)

>j

[1] "45" "INDIA" "TRUE"

> h = list(x,c,q)

>h

[[1]]

[1] 45

[[2]]

[1] "INDIA"

[[3]]

[1] TRUE

> h[1]

[[1]]

[1] 45

> mode(f[1])

[1] "numeric"

> mode(h[1])

[1] "list"

>
> str(h)

List of 3

$ : num 45

$ : chr "INDIA"

$ : logi TRUE

> str(h[2])

List of 1

$ : chr "INDIA"

> letters

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

[14] "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"

> LETTERS

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"

[14] "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"

> mode (letters)

[1] "character"

> is.character (letters)

[1] TRUE

> str(letters)

chr [1:26] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" ...

> letters = 12

> letters

[1] 12 Be careful in using this as it converts it into numeric

n = c(9,25,35,-01,100)
> a= sqrt(n)

Warning message:

In sqrt(n) : NaNs produced

>a

[1] 3.00000 5.00000 5.91608 NaN 10.00000

> is.na(a)

[1] FALSE FALSE FALSE TRUE FALSE

> is.finite(n)

[1] TRUE TRUE TRUE TRUE TRUE

> is.infinite(n)

[1] FALSE FALSE FALSE FALSE FALSE

You might also like