Do You Use R? Here's a Tool for Importing Campbell Scientific Data

por Dirk Baker | Actualizado: 02/15/2017 | Comentarios: 0


Data Analysis

Temas Blog

Buscar en el Blog

Suscribirse al Blog

Configure sus preferencias para la recepción de notificaciones por email cuando se publiquen nuevos artículos en el blog que coincidan con sus áreas de interés.

Entre su dirección email:

Sugerir un Artículo

¿Hay algún tema sobre el cual desea saber más? Háganoslo saber. Por favor sea lo más específico que pueda.

Leave this field empty

datalogger data to R

You may be familiar with R for data processing and analysis. But do you know how to easily import data that is in Campbell Scientific’s TOA5 format into R? In this article, I’ll briefly discuss R and TOA5, and then share a function to create an R dataframe from your TOA5.

R – Statistical Computing and Graphics

R is a powerful, open-source software environment for statistical computing and graphics. It is also popular for its ability to produce publication-quality plots with the necessary mathematical symbols and formulas using UNIX, Windows, and MacOS platforms.

Recommended for You: To learn more about R or download R, visit The R Project for Statistical Computing website.

TOA5 File Format

The TOA5 file format is the default format used for data collected from any contemporary Campbell Scientific dataloggers. This format is a simple comma-delimited text file that has a .dat extension.

The TOA5 file format includes a four-line ASCII header with information about the datalogger you’re using to collect your data. In addition, the header describes the data values with variable names and units of measurement—if they are available.

The four lines of the header are the following:

  • The first line contains information about the datalogger, such as the serial number and program name.
  • The second line is what most people would view as the data header as it contains the names of the variables stored in the table.
  • The third line consists of the units for the variables—if they have been defined in the datalogger.
  • In the fourth line, there is an abbreviation for the processing the datalogger performed (for example, sample, average, standard deviation, maximum, minimum, etc.).

Recommended for You: For more information about TOA5, refer to Appendix B of the LoggerNet instruction manual.

How to Import a TOA5 File into R

You can find the complete code for the import function at the end of this section. Simply copy the code into a file or the R command line.  A call to this R function has one required parameter—the file name, as well as one optional parameter—the Return Option (RetOpt):

importCSdata(“filename.dat”, “RetOpt”)

If you just want the data with a simple header, all you need is one line of code like this:

myData ‹- importCSdata(“CR1000_HourlyWeather.dat”)

The optional RetOpt parameter defaults to “data,” as in the line below:

myData ‹- importCSdata(“CR1000_HourlyWeather.dat”, “data”)

When the RetOpt (Return Option) parameter is omitted or is “data,” an R data frame is created with the second line of the raw data file used for the names. The TIMESTAMP is converted to what R recognizes as a date and time stamp so that this information can be used in graphing, time-series analysis, or the aggregation of data based on date or time.

Because there is a lot of good information in the header of the TOA5 file, you may also want to simply import the header as documentation for your process by using the syntax below:

myData.Info ‹- importCSdata(“CR1000_HourlyWeather.dat”, “info”)

The complete code for the import function:

importCSdata ‹- function(filename,RetOpt="data"){
		# bring in entire header of CSI TOA5 data file for metadata ‹- scan(file=filename,nlines=4,what=character(),sep="\r")
	} else {
		# second line of header contains variable names
		header ‹- scan(file=filename,skip=1,nlines=1,what=character(),sep=",")
		# bring in data ‹- read.table(file=filename,skip=4,header=FALSE, na.strings=c("NAN"),sep=",")
		names( ‹- header
		# add column of R-formatted date/timestamps$TIMESTAMP ‹- as.POSIXlt(strptime($TIMESTAMP,"%Y-%m-%d %H:%M:%S"))

You can download this code.


I hope you find this function useful in your data processing and analyses. Feel free to post your comments about your experience, or your suggestions, below.

Happy analyzing!

Credits: The R logo is © 2016 The R Foundation. The logo was used without modification and with permission in accordance with the terms of the Creative Commons Attribution-ShareAlike 4.0 International license (CC-BY-SA 4.0).

Compartir este artículo

Acerca del autor

Dr. Dirk V. Baker is an Application Research Scientist in the Environmental Group at Campbell Scientific, Inc. Areas of interest include ecology, agriculture, and meteorology—among others. He has a bachelor's degree in wildlife biology and a doctorate in weed science, both from Colorado State University. Dirk’s graduate and postdoctoral research centered around measuring and modeling wind-driven plant dispersal.

Ver todos los artículos de este autor.


J. Magnin | 02/15/2017 at 03:20 AM


This article is quite useful, although your code has some mistakes: you use 2 variables names for the same object ( / & /, so the function stops & returns an error.

By the way, the way you use scan & read.table gave me an idea for enhanced functions to read TOA5 data (and why not, later, other data formats ?). I would like to publish that work, as soon as it will be done, on the CRAN (maybe under GPL V3) to make it available for the R users community. For this, I would like to have your authorization for using your code as a basis, if you agree with that idea.



Dirk | 02/15/2017 at 10:33 AM

Thank you for pointing that out, JM!  Looks like I introduced that typo in condensing the code to fit the page.  It's fixed now and a downloadable version added.

I like the idea of expanding and enhancing the function and, as far as I'm concerned, anyone is free to use and modify my code as needed.  I'm interested to learn more about your ideas and possibly collaborating.  You're welcome to contact me at



Please log in or register to comment.