R programming language

Learn from examples

R is usually used interactive.  
In order to present examples on web page,
the examples are given as input and output files.
Output files have standard output message removed.
Output files have input file lines listed with preceding >
R -f myfile.r > myfile_r.out 

1. Program to output a string, integer, and floating point exrs1.r source code # exrs1.r example print string, integer, float print("R exrs1.r running") str="my string" print(str) n=13 print(n) cat("n=",n,"\n") # optional and redundant, see output x=37.95e+20 print("x=") # only one item allowed print(x) cat("x=",x,"\n") # many items separated by commas print(paste("x=",x),quote=FALSE) # do not print quote character print("exrs1.r finished") Execution output: ≷ # exrs1.r example print string, integer, float ≷ print("R exrs1.r running") [1] "R exrs1.r running" ≷ str="my string" ≷ print(str) [1] "my string" ≷ n=13 ≷ print(n) [1] 13 ≷ cat("n=",n,"\n") # optional and redundant, see output n= 13 ≷ x=37.95e+20 ≷ print("x=") # only one item allowed [1] "x=" ≷ print(x) [1] 3.795e+21 ≷ cat("x=",x,"\n") # many items separated by commas x= 3.795e+21 ≷ print(paste("x=",x),quote=FALSE) # do not print quote character [1] x= 3.795e+21 ≷ print("exrs1.r finished") [1] "exrs1.r finished" 2. commands to execute the source code at a minimum, Windows, Linux, MacOSX. Windows: R interactive or R -f exrs1.r Linux: R interactive or R -f exrs1.r MacOSX R -f exrs1.r 3. You must be able to declare variables and arrays and matrix and lists exrs3.r source code # exrs3.r example declare and use variable, vector, matrix, list print("R exrs3.r running example variable, vector, matrix, list") row = 5 # variable col = 5 # Create vector with values v3 = matrix(data=c(1.1, 2.2, 3.3), nrow=3, ncol=1, byrow=TRUE) print(v3) #Create vector of zeros v4z = matrix(0,4,1); print(v4z) # Create matrix with values m22 = matrix(data=c(1.1, 1.2, 2.1, 2.2), nrow=2, ncol=2, byrow=TRUE) print(m22) # Create a row by col matrix with zeroes mymat = matrix(0,row,col) for(i in 1:row) # compute values for matrix { for(j in 1:col) { mymat[i,j] = 1.0/(i+j) } } print(mymat) # Create and use a list a <- 1:4 print(a) a <- append(a,5,6) print(a) b <- c(a)+10 # add to all "c" function print(b) b=seq(from=1, to=25, by=6) print(b) cat("b=",b,"\n") print("exrs3.r finished") Execution output: > # exrs3.r example declare and use variable, vector, matrix, list > print("R exrs3.r running example variable, vector, matrix, list") [1] "R exrs3.r running example variable, vector, matrix, list" > row = 5 # variable > col = 5 > > # Create vector with values > v3 = matrix(data=c(1.1, 2.2, 3.3), nrow=3, ncol=1, byrow=TRUE) > print(v3) [,1] [1,] 1.1 [2,] 2.2 [3,] 3.3 > > #Create vector of zeros > v4z = matrix(0,4,1); > print(v4z) [,1] [1,] 0 [2,] 0 [3,] 0 [4,] 0 > > # Create matrix with values > m22 = matrix(data=c(1.1, 1.2, 2.1, 2.2), nrow=2, ncol=2, byrow=TRUE) > print(m22) [,1] [,2] [1,] 1.1 1.2 [2,] 2.1 2.2 > > # Create a row by col matrix with zeroes > mymat = matrix(0,row,col) > > for(i in 1:row) # compute values for matrix + { + for(j in 1:col) + { + mymat[i,j] = 1.0/(i+j) + } + } > print(mymat) [,1] [,2] [,3] [,4] [,5] [1,] 0.5000000 0.3333333 0.2500000 0.2000000 0.1666667 [2,] 0.3333333 0.2500000 0.2000000 0.1666667 0.1428571 [3,] 0.2500000 0.2000000 0.1666667 0.1428571 0.1250000 [4,] 0.2000000 0.1666667 0.1428571 0.1250000 0.1111111 [5,] 0.1666667 0.1428571 0.1250000 0.1111111 0.1000000 > > # Create and use a list > a <- 1:4 > print(a) [1] 1 2 3 4 > a <- append(a,5,6) > print(a) [1] 1 2 3 4 5 > b <- c(a)+10 # add to all "c" function > print(b) [1] 11 12 13 14 15 > b=seq(from=1, to=25, by=6) > print(b) [1] 1 7 13 19 25 > cat("b=",b,"\n") b= 1 7 13 19 25 > > print("exrs3.r finished") [1] "exrs3.r finished" > 4. You need to be able to have loops, iteration statements exrs4.r source code # exrs4.r example loops, iteration print("R exrs4.r running") n=4 for(i in 1:n) # matrix start with subscript 1 in R, like Fortran { cat("i=",i,"\n") } for(j in 5:7) { cat("j=",j,"\n") } x=0 while(x < 4) { x <- x+1; cat("x=",x,"\n"); } print("exrs4.r finished") Execution output: > # exrs4.r example loops, iteration > print("R exrs4.r running") [1] "R exrs4.r running" > > n=4 > for(i in 1:n) # matrix start with subscript 1 in R, like Fortran + { + cat("i=",i,"\n") + } i= 1 i= 2 i= 3 i= 4 > > for(j in 5:7) + { + cat("j=",j,"\n") + } j= 5 j= 6 j= 7 > > x=0 > while(x < 4) + { + x <- x+1; + cat("x=",x,"\n"); + } x= 1 x= 2 x= 3 x= 4 > > print("exrs4.r finished") [1] "exrs4.r finished" 5. You need if then else conditional statements exrs5.r source code # exrs5.r example if then else print("R exrs5.r running") # the normal comparison == != ≤ ≥ < > # the normal logical && || n = 2 if(n == 2) { print("n == 2") } if(((n < 3) && (n > 1)) || n != 3) { cat("n=",n,"\n") } if(n ≤ 0) { print("n ≤ 0") } # else ??? error # { # print("n positive") # } print("exrs5.r finished") Execution output: > # exrs5.r example if then else > print("R exrs5.r running") [1] "R exrs5.r running" > # the normal comparison == != ≥ ≤ < > > # the normal logical && || > > n = 2 > if(n == 2) + { + print("n == 2") + } [1] "n == 2" > > if(((n < 3) && (n > 1)) || n != 3) + { + cat("n=",n,"\n") + } n= 2 > > if(n ≤ 0) + { + print("n ≤ 0") + } > # else ??? error > # { > # print("n positive") > # } > > print("exrs5.r finished") [1] "exrs5.r finished" 6. You need to be able to create functions, procedures, subroutines. exrs6.r source code # exrs6.r example define and call a function print("R exrs6.r running") mysqr <- function(x) { sqr <- x*x return(sqr) } xx <- mysqr(7.0) cat("xx=",xx,"\n") print("exrs6.r finished") Execution output: > # exrs6.r example define and call a function > print("R exrs6.r running") [1] "R exrs6.r running" > mysqr <- function(x) + { + sqr <- x*x + return(sqr) + } > > xx <- mysqr(7.0) > cat("xx=",xx,"\n") xx= 49 > > print("exrs6.r finished") [1] "exrs6.r finished" 7. You need to be able to read and write files in various formats. exrs7.r source code # exrs7.r print("R exrs7.r running") # d <- read.table("mydata.txt",sep="\t",col.names=c("id", "name"), # fill=FALSE,strip.white=TRUE) d <- read.table("exrs7.txt",sep="\t") print(d) sink("exrs7.txt") cat("hello\n") cat("world\n") n = 7 cat("n=",n,"\n") sink() file.show("exrs7.txt") print("exrs7.r finished") Execution output: > # exrs7.r > print("R exrs7.r running") [1] "R exrs7.r running" > > # d <- read.table("mydata.txt",sep="\t",col.names=c("id", "name"), > # fill=FALSE,strip.white=TRUE) > d <- read.table("exrs7.txt",sep="\t") > print(d) V1 1 hello 2 world 3 7 > > sink("exrs7.txt") > cat("hello\n") > cat("world\n") > n = 7 > cat("n=",n,"\n") > sink() > > file.show("exrs7.txt") hello world n= 7 > print("exrs7.r finished") [1] "exrs7.r finished" A few other R programs test_sort.r source code test_sort_r.out output factorial.r source code factorial_r.out output matrix.r source code matrix_r.out output

Last updated 2/9/2022