
More complex math steps are handled in separate functions or external scripts.
> fun<-function(a,b){
+ c<-a+b
; the “+” indicates the user input wrapped into next line
+ return(c)
+ }
> fun(2,2)
[1] 4
R will read a data file from the X16-5 data logger using the “read.table” function.
> data<-read.table(“d:\\GCDC\\data-001.csv”, sep=”,”,comment=”;”, fill=TRUE)
“data” is a matrix of 4 columns containing the time, Ax,Ay,Az values from the file. Values within the
matrix are accessed as follows:
> data[100,2] ;row 100, column 2
[1] 101
> a*data[100,2]
[1] 404
The raw data is converted and assigned to new vectors.
> dataX_g<-data[,2]/2048
;convert the x-axis to g and assign to new vector
> dataY_g<-data[,3]/2048
> dataZ_g<-data[,4]/2048
Now, the acceleration in g's is plotted against the elapsed time.
> plot(data[,1], dataX_g, type=”l”)
; create a line plot of x-axis values
> lines(data[,1], dataY_g, type=”l”, col=”blue”)
; add another line to plot
The converted data can be combined into a new matrix and then exported to a new csv data file.
> output<-array(c(data[,1],dataX_g, dataY_g, dataZ_g), dim=c(length(data[,1]),4))
> write.table(output, “c:\\output_data.csv”, sep=”,”)
An analysis can be automated by saving the commands into an external text file. Use “source” to call
the file and R will execute the script inside workspace.
>source(“d:\\hello_world.r”)
[1] hello world
Documentation of the available commands is accessed using “help” or by using an internet search
engine.
> help(“plot”)
; opens a browser with the help documentation for “plot”
6.2.3 Online Resources for R
Home page for R to download the software:
https://www.r-project.org/
A complete introduction to R at their website:
https://cran.r-project.org/doc/manuals/r-release/R-intro.html
Another good tutorial for R beginners:
http://www.cyclismo.org/tutorial/R/
Gulf Coast Data Concepts
Page 28
X16-5, Rev New