Script 19.7

script_19_7.R

# Script 19.7
x<-3 # Set x to hold the value 1
print(x) # Its value can be displayed simply with the print() command
x # Or even simply by stating its name
x+x # Simple operations are straight forward
x*x
sqrt(x) # even the square root, using the built-in function sqrt()
y<- -3.4
y
x-y # Calculate the difference between numbers x and y
t<-"Text" # Set t to hold the String "Text"
t
x-t # Attempt to calculate the difference between x and t which fails
typeof(x) # x is a number with double precision
typeof(t) # t is a string with character type
z<- TRUE # Set a BOOLEAN value TRUE to z
z
x-z # Calculate the difference between x and z provides an answer
typeof(z)
typeof(x-z) # The BOOLEAN logical type TRUE is transformed into value 1 in double type 3-1=2

Script 19.10

script_19_10.R

# Script 19.10hair<-c("black","fair","black","black","brown","fair","white","brown","white","bald")f_hair<-factor(hair) # Make a factor from this vectorf_hairlevels(f_hair) # The categories are the levelslevels(hair) # There are no levels in the original vector

Script 19.14

script_19_14.R

# Script 19.14# Read the table with missing entriesread.table("table2.dat") t<-read.table("table2.dat",header=T,sep="\t")print(t)typeof(t) # The data is interpreted as a list of various subtypestypeof(t$Age)typeof(t$Affected)typeof(t$Gender)summary(t) # A summary of the values can be obtained according to the type

Script 19.15

script_19_15.R

# Script 19.15data(euro) # Load the built-in Euro conversion valueseurodata(package="cluster") # List of datasets is accessible from packagesdata(flower, package="cluster") # Built-in data can be loaded from a packagedata(cars)dim(cars)cars[1:10,] # A subset of the dataset can be usedsummary(cars) # and relevant content displayed

Script 19.21

script_19_21.R

# Script 19.21# A normal distribution with a mean of 0 and standard deviation of 1par(mfrow=c(2,1)) # Split the display in 2 rows and 1 columnx<-seq(-5,10,length=100) # Interval from 5 to 10dx<-dnorm(x) # Default normal distributionplot(x,dx,type="l",main="Normal Distribution") # Plot distribution# Another distribution with mean of 4 and standard deviation of 1.5x2<-xdx<-dnorm(x2,mean=4,sd=1.5)plot(x2,dx2,type="l",main="Other Distribution") # Plot distributionabline(v=4,col="red") # Draw vertical line in red at meanabline(v=0,col="blue") # Draw vertical line in blue at 0mtext(text="mean=4, sd=1.5") # Include extra text on top

Script 19.24

script_19_24.R

# Script 19.24# Heatmap representation of the data with automatic construction of # dendrogram to reflect the organisation of the datapar(mfrow=c(2,1))x<-as.matrix(swiss) # Investigate the fertility in Swiss cantonshead(x)heatmap(as.matrix(swiss))

Script 19.25

script_19_25.R

# Script 19.25# Same plot with different colour scheme and added colour on the# sides from a Rainbow palettepar(mfrow=c(2,1))x<-as.matrix(swiss) # Investigate the fertility in Swiss cantonsrc<-rainbow(nrow(x),start=0, end =.3)cc<-rainbow(ncol(x),start=0, end =.3)heatmap(x,col=cm.colors(256),scale="column",RowSideColors=rc, ColSideColors=cc, xlab="Specification variables", ylab="Cantons",main="Heatmap")