Codes in R
Codes in R
#and operator(&)
x<-12
x<-17
#OR operator
y<-4
y<-14
#Not operator
#$ vs $$,| vs ||
c(TRUE,TRUE,FALSE)&& c(TRUE,FALSE,FALSE)
c(TRUE,TRUE,FALSE)|| c(TRUE,FALSE,FALSE)
2.Strings
r<-c("11","22","33","44")
v<-c(5.5,2.3,1,0,0.8)
v[1]
v[1:4]
mat
v>1
#concentrating string(value written inside single or double quotes are called string.e.g;"hello")
#valid strings
print(d)
#invalid strings
print(e)
#manipulation
abc<-"hello man"
def<-"how"
print(paste(abc,def,ghi))
print(paste(abc,def,ghi,sep="-"))
print(paste(abc,def,ghi,sep=","))
3.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
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)
#Margins area
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")
mtext("par(mar=c(b,l,t,r))",side=3,line=1,cex=1,col = "forestgreen")
box("figure",col="forestgreen")
adj
#Note the 'outer=true'command moves us from the figure margins to the outer margins.
box("outer",col="orange")
Note: shift ctrl then enter key used to run the whole code.