0% found this document useful (0 votes)
4 views4 pages

Practical 6

Uploaded by

vanshsingla079
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)
4 views4 pages

Practical 6

Uploaded by

vanshsingla079
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/ 4

PRACTICAL 6

mow;unmow;data3;data7;data8
[1] 12 15 17 11 15
[1] 8 9 7 9 NA
[1] 6 7 8 7 6 3 8 9 10 7 6 9
[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
[14] 8.0 21.0
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
[12] "Dec"
> grass.list=list(mow,unmow,data3,data7,data8)
> grass.list
[[1]]
[1] 12 15 17 11 15

[[2]]
[1] 8 9 7 9 NA

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

[[4]]
[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
[14] 8.0 21.0

[[5]]
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
[12] "Dec"

> names(grass.list)=c('mow','unmow','data3','data7','months')
> my.names=c('mow','unmow','data3','data7','months')
> names(grass.list)=my.names
> grass.list
$mow
[1] 12 15 17 11 15

$unmow
[1] 8 9 7 9 NA

$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
[14] 8.0 21.0

$months
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
[12] "Dec"

> #(B) Making Data Frames:


> # my.frame=data.frame(item1,item2,item3,..)
> sample1=c(5,6,9,12,8)
> sample1
[1] 5 6 9 12 8
> sample2=c(7,9,13,10,NA)
> sample1;sample2
[1] 5 6 9 12 8
[1] 7 9 13 10 NA
> my.frame=data.frame(sample1,sample2)
> my.frame
sample1 sample2
1 5 7
2 6 9
3 9 13
4 12 10
5 8 NA
> #Let's try one more example
> response=c(5,6,9,12,8,7,9,13,10)
> predictor=c(rep('open',5),rep('closed',4)) #rep(item,times)
> response;predictor
[1] 5 6 9 12 8 7 9 13 10
[1] "open" "open" "open" "open" "open" "closed" "closed"
[8] "closed" "closed"
> my.frame2=data.frame(response,predictor)
> my.frame2
response predictor
1 5 open
2 6 open
3 9 open
4 12 open
5 8 open
6 7 closed
7 9 closed
8 13 closed
9 10 closed
> #length() command can be used to extend or trim a vector
> mow;unmow
[1] 12 15 17 11 15
[1] 8 9 7 9 NA
> length(unmow)=5
> mow;unmow
[1] 12 15 17 11 15
[1] 8 9 7 9 NA
> length(unmow)=4
> mow;unmow
[1] 12 15 17 11 15
[1] 8 9 7 9
> length(unmow)=length(mow) #length(short.vector)=length(long.vector)
> mow;unmow
[1] 12 15 17 11 15
[1] 8 9 7 9 NA
> #(D)Matrix Objects:
> #cbind() to use the samples as columns
> sample1;sample2
[1] 5 6 9 12 8
[1] 7 9 13 10 NA
> cmat=cbind(sample1,sample2)
> cmat
sample1 sample2
[1,] 5 7
[2,] 6 9
[3,] 9 13
[4,] 12 10
[5,] 8 NA
> #rbind to use the samples as rows
> rmat=rbind(sample1,sample2)
> rmat
[,1] [,2] [,3] [,4] [,5]
sample1 5 6 9 12 8
sample2 7 9 13 10 NA
> sample3
[1] "a" "b" "c" "d" "e"
> mix.mat=cbind(sample1,sample2,sample3)
> mix.mat
sample1 sample2 sample3
[1,] "5" "7" "a"
[2,] "6" "9" "b"
[3,] "9" "13" "c"
[4,] "12" "10" "d"
[5,] "8" NA "e"
> #all the data items are convertd to characters; check using str()
> str(mix.mat)
chr [1:5, 1:3] "5" "6" "9" "12" "8" "7" "9" "13" "10" NA "a" "b" ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "sample1" "sample2" "sample3"
> #if you want to extract numbers from a "mixed" matrix
> #you must force the items to be numeric like so:
> as.numeric(mix.mat[,1])
[1] 5 6 9 12 8
> #you might attempt to overwrite a column and
> #try to force the matrix to assume a numerical value:
> mix.mat[,1]=as.numeric(mix.mat[,1])
> mix.mat
sample1 sample2 sample3
[1,] "5" "7" "a"
[2,] "6" "9" "b"
[3,] "9" "13" "c"
[4,] "12" "10" "d"
[5,] "8" NA "e"
> #matrix() command can also be used for making matrix objects
> sample1;sample2
[1] 5 6 9 12 8
[1] 7 9 13 10 NA
> all.samples=c(sample1,sample2)
> all.samples
[1] 5 6 9 12 8 7 9 13 10 NA
> mat=matrix(all.samples,nrow = 2)
> mat
[,1] [,2] [,3] [,4] [,5]
[1,] 5 9 8 9 10
[2,] 6 12 7 13 NA
> mat=matrix(all.samples,ncol = 2)
> mat
[,1] [,2]
[1,] 5 7
[2,] 6 9
[3,] 9 13
[4,] 12 10
[5,] 8 NA
> cnam=c('Sample1','Sample2')
> rnam=c('Site1','Site2','Site3','Site4','Site5')
> mat=matrix(all.samples,ncol = 2,dimnames = list(rnam,cnam))
> mat
Sample1 Sample2
Site1 5 7
Site2 6 9
Site3 9 13
Site4 12 10
Site5 8 NA

>

You might also like