#Practical:6 #Constructing Data Objects: # (A) Making Lists

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

#Practical:6

#Constructing Data Objects:


#(A) Making Lists:
mow;unmow;data3;data7;data8
grass.list=list(mow,unmow,data3,data7,data8)
grass.list
names(grass.list)=c('mow','unmow','data3','data7','months')
my.names=c('mow','unmow','data3','data7','months')
names(grass.list)=my.names
grass.list
#(B) Making Data Frames:
# my.frame=data.frame(item1,item2,item3,..)

sample1=c(5,6,9,12,8)
sample1
sample2=c(7,9,13,10,NA)
sample1;sample2
my.frame=data.frame(sample1,sample2)
my.frame

#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
my.frame2=data.frame(response,predictor)
my.frame2

#length() command can be used to extend or trim a vector

mow;unmow
length(unmow)=5
mow;unmow
length(unmow)=4
mow;unmow
length(unmow)=length(mow) #length(short.vector)=length(long.vector)
mow;unmow

#(D)Matrix Objects:
#cbind() to use the samples as columns

sample1;sample2
cmat=cbind(sample1,sample2)
cmat

#rbind to use the samples as rows

rmat=rbind(sample1,sample2)
rmat

sample3
mix.mat=cbind(sample1,sample2,sample3)
mix.mat
#all the data items are convertd to characters; check using str()

str(mix.mat)
#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])

#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

#matrix() command can also be used for making matrix objects

sample1;sample2
all.samples=c(sample1,sample2)
all.samples

mat=matrix(all.samples,nrow = 2)
mat

mat=matrix(all.samples,ncol = 2)
mat

cnam=c('Sample1','Sample2')
rnam=c('Site1','Site2','Site3','Site4','Site5')
mat=matrix(all.samples,ncol = 2,dimnames = list(rnam,cnam))
mat

You might also like