2.1 Describe how to insert a value between two elements of a vector at a given position by using the “append” function (use the help system to find out). Without “append”, how would you do it?

    The following script shows how to use “append” function to add a value between two elements of a vector at a given position:
x <- c(1,2,3,4)
append(x, 3.5, after = 3)
[1] 1.0 2.0 3.0 3.5 4.0
y <- c(1,2,3,5)
append(y, 4, after = 3)
[1] 1 2 3 4 5
z <- c("tony", "toni", "tone")
append(z, "babyface", after = 2)
[1] "tony"     "toni"     "babyface" "tone"    
    In the case without using “append” function, I created a function “insert”. Basically, this new function will insert a new value in a targeted position in a vector. As seen in the code, the newly inserted value will replace the old value, and the old value will shift to the next position.
insert <- function(v, e, pos) {
         return(c(v[1: (pos-1)], e, v[(pos):length(v)]))
}

v <- c(5,6,7,9)

insert(v, 8, 4)
[1] 5 6 7 8 9
# what if we want to insert a value containing a decimal

b <- c(11,12,13,14)
insert(b, 12.5, 3)
[1] 11.0 12.0 12.5 13.0 14.0
# working on characters
n <- c("noctis", "prompto", "ignis", "gladio")
insert(n, "regis", 2)
[1] "noctis"  "regis"   "prompto" "ignis"   "gladio" 

2.2 Write the built-in dataset “thuesen” to a tab-separated text file with write.table. View it with a text editor. Change the NA to “.” (period), and read the changed file back into R with a suitable command. Also try importing the data into other applications of your choice and exporting them to a new file after editing. You may have to remove row names to make this work.

# Write the built-in dataset "thuesen" to a tab-separated text file with write.table
setwd("C:/research/statistics")
library(ISwR)
write.table(thuesen, file = "thuesen.txt", row.names = F, sep = "\t")

# a way to automate the process of replacing NA to ".", save it and read it in R:

# replace
thuesen.tab.delim <- readLines("thuesen.txt")
thuesen.tab.delim <- sub("NA", ".", thuesen.tab.delim)

# save it
writeLines(thuesen.tab.delim, "thuesen_period.txt")

# this is how it looks like now
cat(readLines("thuesen_period.txt"), sep = "\n")
"blood.glucose" "short.velocity"
15.3    1.76
10.8    1.34
8.1 1.27
19.5    1.47
7.2 1.27
5.3 1.49
9.3 1.31
11.1    1.09
7.5 1.18
12.2    1.22
6.7 1.25
5.2 1.19
19  1.95
15.1    1.28
6.7 1.52
8.6 .
4.2 1.12
10.3    1.37
12.5    1.19
16.1    1.05
13.3    1.32
4.9 1.03
8.8 1.12
9.5 1.7
# load it in R as a data frame
thuesen.period <- read.delim("thuesen_period.txt", na.strings = ".")  # interpret the period as NA
                                                                      # to load the correct data type    
thuesen.period
   blood.glucose short.velocity
1           15.3           1.76
2           10.8           1.34
3            8.1           1.27
4           19.5           1.47
5            7.2           1.27
6            5.3           1.49
7            9.3           1.31
8           11.1           1.09
9            7.5           1.18
10          12.2           1.22
11           6.7           1.25
12           5.2           1.19
13          19.0           1.95
14          15.1           1.28
15           6.7           1.52
16           8.6             NA
17           4.2           1.12
18          10.3           1.37
19          12.5           1.19
20          16.1           1.05
21          13.3           1.32
22           4.9           1.03
23           8.8           1.12
24           9.5           1.70
summary(thuesen.period)
 blood.glucose    short.velocity 
 Min.   : 4.200   Min.   :1.030  
 1st Qu.: 7.075   1st Qu.:1.185  
 Median : 9.400   Median :1.270  
 Mean   :10.300   Mean   :1.326  
 3rd Qu.:12.700   3rd Qu.:1.420  
 Max.   :19.500   Max.   :1.950  
                  NA's   :1      
# last but not least, the following commands are for importing/exporting .csv file
# import
read.csv(file = "xxxx.csv", header = T, sep = ",")

# export
write.csv(your.data.frame, "xxxx.csv")