#The command line
1+1
runif(10)
2*3
6/2
exp(4)

x <-3
#or
x=3
x+4

#Vectors
x=c(2,3,1,4)
x
seq(-1,1,0.1)
rep(1,5)
rep(c(1,2),5)
x=c(2,3,1,4)
y=c(1,4,5,2)
x+y
3*x
sum(x)
sum(x)/length(x)
mean(x)
sort(x)
x[2]
x[c(2,4)]


#Character vectors
c("pink","blue", "green")

#Logical vectors
c(F,T,T,F)
x=c(2,1,6,7,3)
x<5


#Dataframes
height=c(155,177,164,180)
weight=c(65,83,74,115)
d=data.frame(height,weight)
d

#Indexing
d[1,2]
d[1,]
d[,1]
d[1:3,]
d[d$height>164,]
d$height>164

#More on Dataframes
Puromycin
Puromycin$rate
tr.cells=Puromycin$rate[Puromycin$state=="treated"]
tr.cells
untr.cells=Puromycin$rate[Puromycin$state=="untreated"]
untr.cells

#Attach
attach(Puromycin)
detach(Puromycin)

#Subsets of the dataframe
P.subset=subset(Puromycin,rate<100)
P.subset

#sapply
sapply(Puromycin,mean,na.rm=T)

#Matrices
matrix(c(1,3,2,7,5,2),nrow=2)
matrix(c(1,3,2,7,5,2),nrow=2,byrow=T)


#Creating Functions
f=function(x1,x2){
x1bar=mean(x1)
x2bar=mean(x2)
mean=x1bar+x2bar
}
data1=c(2,5,7,3,2)
data2=c(1,3,2,8,3)
mean.value=f(data1,data2)
mean.value

#Plotting points and graphs of functions
x=c(2,4,6,1,3,5,4,5,3)
y=c(1,6,8,9,2,3,2,1,2)
plot(x,y,main="Scatterplot",xlab="x-values",ylab="y-values",col="blue")
points(3,9,col="red")
points(2,8,pch=2,col="green")

x=seq(-5,5,0.1)
f=function(x){x^(2)}
plot(x,f(x),type="l",col="red")
axis(1, at=1.5, labels="x*")
axis(1,at=seq(-5,5,1))
text(3,10,labels="text")

#for loops
x=numeric(10)
for (i in 1:10){x[i]=prod(1-(1:i)/9)}
i=c(1:10)
data.frame(i,x)

#Reading data from text files z=read.table("C:/Introduction/flowers.txt",header=T)
z
z=read.delim("C:/Introduction/flowers.txt")

#Built-in datasets
data()
data(Orange)

#Library and Packages
library(boot)
library(plotrix)

#Help in R
help.start()
help(t.test)
?t.test


