R4_0121212

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

PRACTICAL 4:- Software “R”

> #Practical:4
> #Manipulating Objects:
> #(a)Manipulating Vectors;
> #(i)Selecting and Displaying parts of a Vector
> data1
[1] 3 5 7 5 3 2 6 8 5 6 9
> data1[3]
[1] 7
> data1[-4]
[1] 3 5 7 3 2 6 8 5 6 9
> data1[2:5]
[1] 5 7 5 3
> data1[c(2,4,5,9)]
[1] 5 5 3 5
> data1[data1>5]
[1] 7 6 8 6 9
> data1[data1>3|data1<2]
[1] 5 7 5 6 8 5 6 9
> max(data1)
[1] 9
> min(data1)
[1] 2
> length(data1)
[1] 11
> seq(1,10,2,)
[1] 1 3 5 7 9
> seq(1,100,3)
[1] 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67
[24] 70 73 76 79 82 85 88 91 94 97 100
> data1[seq(1,length(data1),2)]
[1] 3 7 3 6 5 9
> data1[-1:-3]
[1] 5 3 2 6 8 5 6 9
> seq(1,length(data1),2)
[1] 1 3 5 7 9 11
>
> data5
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> data5[3]
[1] "Mar"
> data5[-1]
[1] "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> data5[4:7]
[1] "Apr" "May" "Jun" "Jul"
> data5[-1:-6]
[1] "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
>
> #(ii)Sorting and Rearranging a Vector
> data1
[1] 3 5 7 5 3 2 6 8 5 6 9
> sort(data1)
[1] 2 3 3 5 5 5 6 6 7 8 9
> sort(data1,decreasing= TRUE)
[1] 9 8 7 6 6 5 5 5 3 3 2
> order(data1)
[1] 6 1 5 2 4 9 7 10 3 8 11
> rank(data1)
[1] 2.5 5.0 9.0 5.0 2.5 1.0 7.5 10.0 5.0 7.5 11.0
> rank(data1,ties.method="first")
[1] 2 4 9 5 3 1 7 10 6 8 11
> rank(data1,ties.method= "max")
[1] 3 6 9 6 3 1 8 10 6 8 11
> rank(data1,ties.method= "min")
[1] 2 4 9 4 2 1 7 10 4 7 11
> ?NA
> unmow
[1] 8 9 7 9 NA
> sort(unmow)
[1] 7 8 9 9
> sort(unmow,decreasing = TRUE)
[1] 9 9 8 7
> sort(unmow,na.last = NA)
[1] 7 8 9 9
> sort(unmow,na.last = TRUE)
[1] 7 8 9 9 NA
> sort(unmow,na.last = FALSE)
[1] NA 7 8 9 9
>
> unmow
[1] 8 9 7 9 NA
> order(unmow)
[1] 3 1 2 4 5
> order(unmow,na.last = NA)
[1] 3 1 2 4
> order(unmow,na.last= FALSE)
[1] 5 3 1 2 4
> unmow
[1] 8 9 7 9 NA
> order(unmow)
[1] 3 1 2 4 5
> rank(unmow)
[1] 2.0 3.5 1.0 3.5 5.0
> unmow
[1] 8 9 7 9 NA
> rank(unmow,ties.method = 'first')
[1] 2 3 1 4 5
> rank(unmow,ties.method = 'average')
[1] 2.0 3.5 1.0 3.5 5.0
> rank(unmow,ties.method = 'max')
[1] 2 4 1 4 5
> rank(unmow,ties.method = 'random',na.last = 'keep')
[1] 2 3 1 4 NA
> dat.na
[1] 2 5 4 NA 7 3 9 NA 12
> rank(dat.na,na.last = 'keep')
[1] 1 4 3 NA 5 2 6 NA 7
>
> #(iii) Returing Logical Values from a vector
> data1
[1] 3 5 7 5 3 2 6 8 5 6 9
> data1==5
[1] FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
> data1<4
[1] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
> data1>3
[1] FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
>
> which(data1==5)
[1] 2 4 9
>
> data2
[1] 3 5 7 5 3 2 6 8 5 6 9 4 5 7 3 4
> data2>5
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE
FALSE TRUE FALSE
[16] FALSE
> data2<5
[1] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
FALSE FALSE TRUE
[16] TRUE
> data2>5|data2<8
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
TRUE TRUE
> data8
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> data8=='Feb'|data8=='Apr'
[1] FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>
> #(b)Manipulating Matrix and Data Frames:
> #(i)Selecting and Displaying Parts of a Matrix or Data frame:
>
> mf#data frame
Length Speed Algae NO3 BOD
1 20 12 40 2.25 200
2 21 14 45 2.15 180
3 22 12 45 1.75 135
4 23 16 80 1.95 120
5 21 20 75 1.95 110
6 20 21 65 2.75 120
7 19 17 65 1.85 95
8 16 14 65 1.75 168
9 15 16 35 1.95 180
10 14 21 30 2.35 195
11 21 21 65 2.35 158
12 21 26 70 2.35 145
13 21 11 85 2.05 140
14 20 9 70 1.85 145
15 19 9 35 1.75 165
16 18 11 30 1.45 187
17 17 17 50 1.35 190
18 19 15 60 2.05 157
19 21 19 70 1.25 90
20 13 21 25 1.05 235
21 16 22 35 2.55 200
22 25 9 85 2.85 55
23 24 11 80 2.95 87
24 23 16 80 2.85 97
25 22 15 75 1.75 95
> str(mf)
'data.frame': 25 obs. of 5 variables:
$ Length: int 20 21 22 23 21 20 19 16 15 14 ...
$ Speed : int 12 14 12 16 20 21 17 14 16 21 ...
$ Algae : int 40 45 45 80 75 65 65 65 35 30 ...
$ NO3 : num 2.25 2.15 1.75 1.95 1.95 2.75 1.85 1.75 1.95 2.35 ...
$ BOD : int 200 180 135 120 110 120 95 168 180 195 ...
> class(mf)
[1] "data.frame"
> mf[3,3] #item from third row and third column
[1] 45
> mf[3,"Algae"]
[1] 45
> mf[,1] #first column and all rows
[1] 20 21 22 23 21 20 19 16 15 14 21 21 21 20 19 18 17 19 21 13 16 25 24 23 22
> mf[3,]
Length Speed Algae NO3 BOD
3 22 12 45 1.75 135
> mf[c(1,3,5,7),] #first,third,fifth,seventh row and all colmns
Length Speed Algae NO3 BOD
1 20 12 40 2.25 200
3 22 12 45 1.75 135
5 21 20 75 1.95 110
7 19 17 65 1.85 95
> mf[c(1,3,5,7),-4] #rows mentioned in c() and all colmns except fourth
Length Speed Algae BOD
1 20 12 40 200
3 22 12 45 135
5 21 20 75 110
7 19 17 65 95
> mf[c(1,3,5,7),'Algae'] #using name rather than a simple value
[1] 40 45 75 65
> mf[3] #single value in [] will be interpreted as column.
Algae
1 40
2 45
3 45
4 80
5 75
6 65
7 65
8 65
9 35
10 30
11 65
12 70
13 85
14 70
15 35
16 30
17 50
18 60
19 70
20 25
21 35
22 85
23 80
24 80
25 75
>
> bird #matrix
Garden Hedgerow Parkland Pasture Woodland
Blackbird 47 10 40 2 2
Chaffinch 19 3 5 0 2
Great Tit 50 0 10 7 0
House Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Song Thrush 4 0 6 0 0
> str(bird) #Compactly display the internal structure of an R object.
num [1:6, 1:5] 47 19 50 46 9 4 10 3 0 16 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:6] "Blackbird" "Chaffinch" "Great Tit" "House Sparrow" ...
..$ : chr [1:5] "Garden" "Hedgerow" "Parkland" "Pasture" ...
> class(bird) #to know what kind of object are dealing with.
[1] "matrix" "array"
> bird[2,] #second row and all the columns
Garden Hedgerow Parkland Pasture Woodland
19 3 5 0 2
> bird[,4] #all rows but only fourth column
Blackbird Chaffinch Great Tit House Sparrow Robin Song Thrush
2 0 7 4 0 0
> bird[c('Robin','Blackbird'),] #named rows and all columns
Garden Hedgerow Parkland Pasture Woodland
Robin 9 3 0 0 2
Blackbird 47 10 40 2 2
> bird[3,1] #item from third row and first column
[1] 50
> bird[4] #the single value in [] gives a single value rather than the colmnn
[1] 46
>
>
> #(ii)Sorting and Rearranging a Matrix or Data frame:
> #matrix is essentially a single vector of data that contains information
> #about how to split it up into rows and columns.
>
> bird
Garden Hedgerow Parkland Pasture Woodland
Blackbird 47 10 40 2 2
Chaffinch 19 3 5 0 2
Great Tit 50 0 10 7 0
House Sparrow 46 16 8 4 0
Robin 9 3 0 0 2
Song Thrush 4 0 6 0 0
> sort(bird)
[1] 0 0 0 0 0 0 0 0 0 2 2 2 2 3 3 4 4 5 6 7 8 9 10 10 16 19 40 46 47 50
> order(bird)
[1] 9 12 17 20 23 24 27 28 30 19 25 26 29 8 11 6 22 14 18 21 16 5 7 15 10 2 13 4
1 3
> rank(bird)
[1] 29.0 26.0 30.0 28.0 22.0 16.5 23.5 14.5 5.0 25.0 14.5 5.0 27.0 18.0 23.5 21.0 5.0
19.0 11.5
[20] 5.0 20.0 16.5 5.0 5.0 11.5 11.5 5.0 5.0 11.5 5.0
> sort(bird[,1]) #to sort first column only
Song Thrush Robin Chaffinch House Sparrow Blackbird Great Tit
4 9 19 46 47 50
> order(bird[,1])#to order first column only
[1] 6 5 2 4 1 3
> order(bird[,5])
[1] 3 4 6 1 2 5
> order(bird[,5],order(bird[,1]))#order 5th col,* the order of first column is used for
ordering the fifth
[1] 3 6 4 5 2 1
> column*,using the 1st col as tie-breaker
Error: unexpected ',' in "column*,"
> grass2
mow unmow
1 12 8
2 15 9
3 17 7
4 11 9
5 15 NA
> sort(grass2) #error as the command needs to operate on a single vector(mow)
Error in xtfrm.data.frame(x) : cannot xtfrm data frames
> load("C:/Users/STUDENT/Desktop/Beginning (1).RData")
> sort(grass2) #error as the command needs to operate on a single vector(mow)
Error in xtfrm.data.frame(x) : cannot xtfrm data frames
> sort(grass2$mow) #error as the command needs to operate on a single vector(mow)
[1] 11 12 15 15 17
> sort(grass2[1,]) #sort first row of df
Error in xtfrm.data.frame(x) : cannot xtfrm data frames
> sort(grass2[$1,]) #sort first row of df
Error: unexpected '$' in "sort(grass2[$"
> sort(grass2[,1]) #sort first column of df
[1] 11 12 15 15 17
> sort(grass2[1,]) #sort first row of df
Error in xtfrm.data.frame(x) : cannot xtfrm data frames
> sort(grass2[,'mow'])#sort 'mow' column of df
[1] 11 12 15 15 17
>
> sort(grass2$mow) #alternate way to write the previous commands
[1] 11 12 15 15 17
> grass2
mow unmow
1 12 8
2 15 9
3 17 7
4 11 9
5 15 NA
> grass2$mow
[1] 12 15 17 11 15
> grass2[,1]
[1] 12 15 17 11 15
> grass2$unmow
[1] 8 9 7 9 NA
> grass2[,2]
[1] 8 9 7 9 NA
> order(grass2) #taking the entire df
Error in xtfrm.data.frame(x) : cannot xtfrm data frames
> order(grass2$mow,grass2[,2])#order the 1st col,using the 2nd col as tie-breaker
[1] 4 1 2 5 3
> with(grass2,order(mow,unmow))#alternate command
[1] 4 1 2 5 3
>
> rank(grass2)
mow unmow <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
6.0 7.5 9.0 5.0 7.5 2.0 3.5 1.0 3.5 10.0
>
> #(C)Manipulating Lists:
> my.list
$mow
[1] 12 15 17 11 15

$unmow
[1] 8 9 7 9

$data3
[1] 6 7 8 7 6 3 8 9 10 7 6 9

$data7
[1] 23.0 17.0 12.5 11.0 17.0 12.0 14.5 9.0 11.0 9.0 12.5 14.5 17.0 8.0 21.0

> my.list$data3
[1] 6 7 8 7 6 3 8 9 10 7 6 9
> my.list$mow
[1] 12 15 17 11 15
>
> str(my.list)
List of 4
$ mow : int [1:5] 12 15 17 11 15
$ unmow: int [1:4] 8 9 7 9
$ data3: num [1:12] 6 7 8 7 6 3 8 9 10 7 ...
$ data7: num [1:15] 23 17 12.5 11 17 12 14.5 9 11 9 ...
> class(my.list)
[1] "list"
>
> sort(my.list)
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) :
'x' must be atomic
> sort(my.list$data3)
[1] 3 6 6 6 7 7 7 8 8 9 9 10
> sort(my.list$mow)
[1] 11 12 15 15 17
> order(my.list$mow,my.list$unmow) #error as arguments length differ
Error in order(my.list$mow, my.list$unmow) : argument lengths differ
> order(my.list$unmow)
[1] 3 1 2 4

You might also like