0% found this document useful (0 votes)
12 views5 pages

Codes in R

Uploaded by

jaweria arshad
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)
12 views5 pages

Codes in R

Uploaded by

jaweria arshad
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/ 5

1.

And OR NOT operators:

#and operator(&)

x<-12

x>5 & x<15 #only true if both true

x<-17

x>5 & x<15 #false otherwise

#OR operator

y<-4

y<5|y>15 #true if atleast one is true

y<-14

y<5|y>15 #only false if both false

#Not operator

is.numeric(5) #result true

! is.numeric(5) #result false

is.numeric("hello") #result false

! is.numeric("hello") #result true

#logical operators and vectors

c(TRUE,TRUE,FALSE)& c(TRUE,FALSE,FALSE) #only true if both are true

c(TRUE,TRUE,FALSE)| c(TRUE,FALSE,FALSE) #only false if both false

!c(TRUE,TRUE,FALSE) #true evaluate false and false evaluate true

#single sign and double sign version

#$ vs $$,| vs ||

c(TRUE,TRUE,FALSE)&& c(TRUE,FALSE,FALSE)

c(TRUE,TRUE,FALSE)|| c(TRUE,FALSE,FALSE)
2.Strings

# create a simple vector

r<-c("11","22","33","44")

r[3] #extract 3rd element

r[-3]#leave 3rd element

r[10]#does not exist

#introduction to indexing in R,the extract function,vector and matrices

v<-c(5.5,2.3,1,0,0.8)

v[1]

v[1:4]

mat<-matrix(1:12, nrow = 4, ncol = 4)

mat

mat[2,2] #extract element of second row and second column

mat[,3] #extract 3rd column and all rows

v[v>1]# all the elements of v which is greater than 1

v>1

#concentrating string(value written inside single or double quotes are called string.e.g;"hello")

#examples of valid and invalid strings.

#"teacher",'hey' are valid strings.

#'student",'hello" are invalid string.

#how to create and manipulate strings using predefined functions.

#valid strings

c<-"single quote'in between double quotes"


print(c)

d<-'double quotes " in between single quotes'

print(d)

#invalid strings

e<-'single quote'inside single quote'

print(e)

#manipulation

abc<-"hello man"

def<-"how"

ghi<-"are you feeling"

print(paste(abc,def,ghi))

print(paste(abc,def,ghi,sep="-"))

print(paste(abc,def,ghi,sep=","))

3.Data frame:

#create data frame

name<-c("anne","peter","nida","hassan","razi")

age<-c(28,30,21,39,"A")

child<-c("false","true","false","true","true")

df<-data.frame(name,age,child)

df

#structure of data frame

#To specify name explicitly

names(df)<-c("name","age","child")

names(df)
4.Simple graphs in R

x=3:5

y=5:7

plot(x,y)

plot(x,y,main="myscatterplot")

plot(x,y,main="myscatterplot",col.main="red")

plot(x,y,main="myscatterplot",col.main="red",xlab="myxlab")

plot(x,y,main="myscatterplot",col.main="red",xlab="myxlab")

plot(x,y,main="myscatterplot",col.main="red",xlab="myxlab",ylab="myylab")

?par

colours()

plot(x,y,main="myscatterplot",col.main="thistle")

polygon(x,y)

5.Inner and outer Margins

#Margins area

par(oma=c(2,2,2,2))#all sides have 3 lines of space

par(mar=c(5,4,4,2)+0.1)

#plot

plot(0:10,0:10,type="p",xlab="sumbal",ylab = "chughtai")

#place text in the plot and color everything plot-related red text

text(8,1,"plot",col = "red",cex = 2)

text(5,5,"plot",col = "red",cex = 2)

text(3,3,"plot",col = "red",cex = 2)

box("figure",col ="red")

#place text in the margins and label the margins,all in forestgreen


mtext("Margins",side=3,line=2,cex=1,col = "forestgreen")

mtext("par(mar=c(b,l,t,r))",side=3,line=1,cex=1,col = "forestgreen")

mtext("line 0",side=3,line=0,adj =0.0,cex = 1,col ="forestgreen")

mtext("line 1",side=3,line=1,adj =1.0,cex = 1,col ="forestgreen")

mtext("line 2",side=3,line=2,adj =1.0,cex = 1,col ="forestgreen")

mtext("line 3",side=3,line=3,adj =1.0,cex = 1,col ="forestgreen")

box("figure",col="forestgreen")

adj

#label the outer margin area and colour it blue

#Note the 'outer=true'command moves us from the figure margins to the outer margins.

mtext("outer margin area",side = 1,line = 1,cex = 2,col = "blue",outer = TRUE)

mtext("par(oma=c(b,l,t,r))",side = 1,line = 2,cex = 1,col = "yellow",outer = TRUE)

mtext("line o",side = 1,line = 0,adj=0.0,cex = 1,col = "blue",outer = TRUE)

mtext("line 1",side = 1,line = 1,adj=0.0,cex = 1,col = "blue",outer = TRUE)

mtext("line 2",side = 1,line = 2,adj=0.0,cex = 1,col = "blue",outer = TRUE)

box("outer",col="orange")

Note: shift ctrl then enter key used to run the whole code.

You might also like