Better filereading and print formating in R I am a beginner using R and I am trying to solve some simple statistics problems and print the output. The following code takes in two datasets, computes the mean, variance and standard deviation. Finally it computes a 95-confidence interval for the standard deviation. 

This is just a sample and my real data consists of a couple of thousand values, the files below are just for illustration. 

Currently I read in my data files as follows

list(as.numeric(unlist(read.table(filename)))


however, I strongly feel there must be a better method? My datafiles
usually consists of rows of data (anywhere from 3 to 10 columns) and about a 1000 rows.

Secondly I find myself doing the following a lot

cat(paste(c("Confidence Intervall for the standard variation: (",
            f(CI.sigma.low[1]),", ",#
            f(CI.sigma.high[1]),")\n"), collapse = ""))


I tried to create a function that does this, however, I ran into problems when dealing with
multiple values. 

I am not looking for feedback on the mathematics. Only syntax, and tips on how to 
improve the overall structure of the code

Code

data_01.txt

34.10
34.21
33.34
35.45
31.56
34.67
36.72
33.16
34.35
34.50


data_02.txt

38.82518 37.61011 39.70940 41.19950 37.91823 39.06883 41.86497 38.87179
39.72824 39.46657 36.42833 39.73950 38.15764 38.49400 37.96093 38.31201
41.23278 37.50835 39.25816 39.72357 37.11235 38.84875 37.88969 38.86277


Main.R

EXERCISE &lt;- 1 # Global variable, 

# Shorthand for printing without " or numbers
p &lt;- function(text,value) {
  cat(paste(c(text,value,"\n"), collapse = ""))
} 

# Rounds number to X decimal places
f &lt;- function(num, decimals=3) {
  round(num*10^decimals)/10^decimals
}

# Fancy title for the problems
problem &lt;- function(index=EXERCISE) {
  w &lt;- strrep("=",80); w.small &lt;- strrep(" ",35)

  cat("\n\n"); cat(w,"\n")
  p(c(w.small,"Problem 3",letters[index],w.small),"")
  cat(w,"\n\n")
  #double &lt;&lt; increments out of current scope
  EXERCISE &lt;&lt;- EXERCISE + 1
}


problem() # Problem 3a

# A list makes it easy to change or add files
filenames = c("data_01.txt",
              "data_02.txt")
kvm &lt;- list()
for (i in 1:length(filenames)) {
  kvm[i] = list(as.numeric(unlist(read.table(filenames[i]))))
}

# Applies the function "length" to each element of "kvm" u.z.w
N &lt;- sapply(kvm, length, simplify = TRUE)
mu &lt;- sapply(kvm, mean, simplify = TRUE)
sigma &lt;- sapply(kvm, sd, simplify = TRUE)

p("Number of values: ",N[1])
p("  Expected value: ",f(mu[1]))
p("        Variance:  ",f(sigma[1]^2))


problem() # Problem 3b

I &lt;- 0.95 # 95-confidence intervall
alpha &lt;- 1 - I

# qchisq is the Chi-squared Distribution
# df= degrees of freedom (frihetsgrader)
# CI = confidence interval
CI.sigma.low = (N-1) * sigma^2 / qchisq(1-alpha/2, df=N-1)
CI.sigma.high = (N-1) * sigma^2 / qchisq(alpha/2, df=N-1)

cat(paste(c("Confidence Intervall for the standard variation: (",
            f(CI.sigma.low[1]),", ",#
            f(CI.sigma.high[1]),")\n"), collapse = ""))


problem() # Problem 3e (T-interval)

I &lt;- 0.95; alpha &lt;- 1 - I

d.f &lt;- sum(N) - length(N)
sigma.p &lt;- sqrt(sum(sigma^2*(N-1))/d.f)

p("Pooled variance: ",f(sigma.p^2))
p("Pooled standard deviation: ",f(sigma.p))

v.p &lt;- qt(1-alpha/2, d.f) * sigma.p * sqrt(sum(1/N))

CI.diff.mu.low &lt;- (mu[1] - mu[2])-v.p 
CI.diff.mu.high &lt;- (mu[1] - mu[2])+v.p 

p1 &lt;- "Confidence Intervall for difference "
p2 &lt;- "between mean values (mu_1-mu_2):"

cat(paste(c("\n",p1,p2,"\n","  (",
            f(CI.diff.mu.low),", ",#
            f(CI.diff.mu.high),")\n"), collapse = ""))
This should not be a complete review, but the base R function round has an optional second argument digits. It works like this


&gt; round(1.234567, digits=1)
[1] 1.2
&gt; round(1.234567, digits=2)
[1] 1.23



Therefore there is no need for your function f.