Below are listed all the scripts as shown in chapter 19 of the book. These are free to copy and paste into your code editor.
Quick links to each script:
Script 19.1
script_19_1.awk, file.dat 
# Script 19.1
# Processing of the ASCIIfile file.tab with tabseparated values
awk '
BEGIN {FS="\t"}
(NR>1){
if ($1>2) {print $1,$2$4 } else {print $1,$3}
n=n+1
}
END {print "This is the End after processing "n" lines."}
' file.dat

script_19_1.out 
[[email protected] Documents]$ ./script_19_1.awk
1 S1 Male 66.3 20 TRUE
2 S2 Male 80.2 21 FALSE 0
3 S3 Female 53.1 29 TRUE 0
4 S4 Female 21.3 18 TRUE 0
5 S5 Female 54.6 NA FALSE 0
6 S6 Female 78.1 23 TRUE 0
This is the End after processing 6 lines.

Script 19.2
script_19_2.py 
# Script 19.2
# Calculate the sum of squares of data in an array
s = 0
data = [3, 1, 2, 5, None, 9, 2]
for d in data:
if not d is None:
s += d * d
print('The sum of squares is ',s)

script_19_2.out 
[[email protected] Documents]$ python script_19_2.py
('The sum of squares is ', 124)

Script 19.3
script_19_3.R 
# Script 19.3
# Plotting a normal distribution
x<seq(4,4,length=100) dx<dnorm(x,mean=0,sd=1) plot(x,dx,type="l",main="Normal Distribution") p1<seq(4,1.5,length=100) dp1<dnorm(p1) p2<seq(1.5,4,length=100) dp2<dnorm(p2) polygon(c(4,p1,1.5),c(0,dp1,0),col="gray")

script_19_3.png 

Script 19.4
script_19_4.R 
# Script 19.4
library() # List packages available, not necessarily active
data(agriculture) # Attempt to load the Agriculture dataset
bannerplot(agnes(agriculture), main = "Bannerplot") # Run bannerplot function
library(cluster) # Load the cluster library and try again
data(agriculture)
bannerplot(agnes(agriculture), main = "Bannerplot")

script_19_4.png


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 builtin function sqrt()
y< 3.4
y
xy # Calculate the difference between numbers x and y
t<"Text" # Set t to hold the String "Text"
t
xt # 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
xz # Calculate the difference between x and z provides an answer
typeof(z)
typeof(xz) # The BOOLEAN logical type TRUE is transformed into value 1 in double type 31=2

script_19_7.out

[[email protected] Documents]$ R
R version 3.3.1 (20160621)  "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64redhatlinuxgnu (64bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for online help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> # Script 19.7
> x print(x) # Its value can be displayed simply with the print() command
[1] 3
>
> x # Or even simply by stating its name
[1] 3
>
> x+x # Simple operations are straight forward
[1] 6
>
> x*x
[1] 9
>
> sqrt(x) # even the square root, using the builtin function sqrt()
[1] 1.732051
>
> y y
[1] 3.4
>
> xy # Calculate the difference between numbers x and y
[1] 6.4
>
> t t
[1] "Text"
>
> xt # Attempt to calculate the difference between x and t which fails
Error in x  t : nonnumeric argument to binary operator
>
> typeof(x) # x is a number with double precision
[1] "double"
>
> typeof(t) # t is a string with character type
[1] "character"
>
> z z
[1] TRUE
>
> xz # Calculate the difference between x and z provides an answer
[1] 2
>
> typeof(z)
[1] "logical"
>
> typeof(xz) # The BOOLEAN logical type TRUE is transformed into value 1 in double type 31=2
[1] "double"
>

Script 19.8
script_19_8.R 
# Script 19.8
v < c(10, 1.6, 1, 2.6, 1.7) # Create a vector of length 5 with set values
v
w<1:10 # Create a vector of length 10
w
v+1 # One can operate on all numbers
v*w # Even between vectors
sqrt(v) # Or more complex operation like square root
mean(v) # Average across the values of the vector
sd(v) # Standard Deviation
sum(v) # Sum
length(v) # Length of the vector

script_19_8.out 
[[email protected] Documents]$ R
R version 3.3.1 (20160621)  "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64redhatlinuxgnu (64bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for online help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> # Script 19.8
> v v
[1] 10.0 1.6 1.0 2.6 1.7
> w w
[1] 1 2 3 4 5 6 7 8 9 10
> v+1 # One can operate on all numbers
[1] 11.0 2.6 2.0 3.6 2.7
> v*w # Even between vectors
[1] 10.0 3.2 3.0 10.4 8.5 60.0 11.2 8.0 23.4 17.0
> sqrt(v) # Or more complex operation like square root
[1] 3.162278 1.264911 1.000000 1.612452 1.303840
> mean(v) # Average across the values of the vector
[1] 3.38
> sd(v) # Standard Deviation
[1] 3.744596
> sum(v) # Sum
[1] 16.9
> length(v) # Length of the vector
[1] 5
>

Script 19.9
script_19_9.R 
# Script 19.9 m<array(2:7,dim=c(2,3)) # Create a 2x3 array containing values from 2 to 7 m m[1,3] # Access a single cell by index m[1,] # Access a single row m[,c(1,3)] # Access a pair of columns

script_19_9.out 
[[email protected] Documents]$ R
R version 3.3.1 (20160621)  "Bug in Your Hair" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64redhatlinuxgnu (64bit)
R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for online help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.
> # Script 19.9 > m<array(2:7,dim=c(2,3)) # Create a 2x3 array containing values from 2 to 7 > m [,1] [,2] [,3] [1,] 2 4 6 [2,] 3 5 7 > m[1,3] # Access a single cell by index [1] 6 > m[1,] # Access a single row [1] 2 4 6 > m[,c(1,3)] # Access a pair of columns [,1] [,2] [1,] 2 6 [2,] 3 7 >

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

script_19_10.out 
[[email protected] Documents]$ R
R version 3.3.1 (20160621)  "Bug in Your Hair" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64redhatlinuxgnu (64bit)
R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for online help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.
> # Script 19.10 > hair<c("black","fair","black","black","brown","fair","white","brown","white", "bald") > f_hair<factor(hair) # Make a factor from this vector > f_hair [1] black fair black black brown fair white brown white bald Levels: bald black brown fair white > levels(f_hair) # The categories are the levels [1] "bald" "black" "brown" "fair" "white" > levels(hair) # There are no levels in the original vector NULL >

Script 19.11
script_19_11.R 
# Script 19.11 res < list(test_name=c("t.test","Fisher_exact"), pval=0.001, df=3, err=c(0.4,0.7,0.9)) # List of 4 components res res$pval # Access the full element pval res$err # Access the full element err res$test_name[1] # Access an element in an element

script_19_11.out 
[[email protected] Documents]$ R
R version 3.3.1 (20160621)  "Bug in Your Hair" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64redhatlinuxgnu (64bit)
R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for online help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.
> # Script 19.11 > res < list(test_name=c("t.test","Fisher_exact"), pval=0.001, df=3, err=c(0.4,0.7,0.9)) # List of 4 components > res $test_name [1] "t.test" "Fisher_exact"
$pval [1] 0.001
$df [1] 3
$err [1] 0.4 0.7 0.9
> res$pval # Access the full element pval [1] 0.001 > res$err # Access the full element err [1] 0.4 0.7 0.9 > res$test_name[1] # Access an element in an element [1] "t.test" >

Script 19.12
script_19_12.R 
# Script 19.12 # Read the table considering the first row as data read.table("table.dat")

script_19_12.out 
[[email protected] Documents]$ R
R version 3.3.1 (20160621)  "Bug in Your Hair" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64redhatlinuxgnu (64bit)
R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for o
Are you sure you want to delete your account?
This cannot be undone.
Thank you for your feedback which will help us improve our service.
If you requested a response, we will make sure to get back to you shortly.
×
Please fill in the required fields in your feedback submission.
×
