#Creating
objects in R
a=c(1,2,3,4)
b=c(4,5,6,1)
c=a+b
c
d=seq(1,20,2)
d
x=c(eleven=11, twelve=12,
thirteen=13, fourteen=14)
x
#changing the name of elements of the vector
names (x)[1:2]=c("onze",
"douze")
x
mode (x)
class (x)
z=c(1,2,3)
#combining x and z into 1 vector
all=c(y,x,z)
all
#using list we can create objects of varying dimensions and types in a
single R object
mylist<-list(c(1,2,3),
"dog", TRUE, c(9,8,7))
mylist
sapply(mylist,mode)
mylist[[2]]
mylist[[1]]
#you can try it on any other object
x[2]
x[1]
#working with matrices
matrix=matrix(c(1,2,3,
11,12,13),nrow=3, ncol=2, byrow=T, dimnames=list(c("r1",
"r2", "r3"), c("c1", "c2")))
matrix
names_matrix=list(c("row1",
"row2", "row3"), c("col1", "col2"))
matrix1=matrix(c(1,2,3,
11,12,13),nrow=3, ncol=2, byrow=T, dimnames=names_matrix)
sbp=c(144, 138, 162, 170, 158, 162,
140, 128, 135, 116, 136, 120, 160, 144, 125, 220, 145, 142, 124, 154, 150, 110,
130, 114, 124, 142, 120, 158, 130, 175)
age=c(39, 45, 65, 67, 67, 64, 59, 42,
45, 20, 36, 39, 44, 63, 25, 47, 47, 46, 42, 56, 56, 34, 48, 17, 19, 50, 21, 33,
29, 69)
matrix_age=matrix(age, nrow=5,
ncol=6, byrow=T)
matrix_sbp=matrix(sbp, nrow=5,
ncol=6, byrow=T)
matrix_age
matrix_sbp
#creating an identity matrix of size 5x5
n=c(5)
I=matrix(0,nrow=n, ncol=n)
I[row(I)==col(I)]=1
I