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

von Dirk Baker | Aktualisiert: 10/03/2017 | Kommentare: 3


Data Analysis

Blog Themen

Suchen Sie im Blog

Blog abonieren

Erhalten Sie eine Email wenn ein neuer Artikel veröffentlicht wird. Wählen Sie die Themen die Sie interessieren.

Geben Sie Ihre Emailadresse ein:

Artikel vorschlagen

Gibt es ein Thema zu dem Sie mehr erfahren möchten?

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).

Artikel teilen

Über den Author

dirk baker 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.

Alle Artikel von diesem Author anzeigen.


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



ariklee | 02/23/2018 at 12:57 PM

Awesome! Great to see R represented in a Campbell blog. I've written a short R script also to read TOA5 data, it's pretty handy to separate out the 4-line header from the data.

A quick search on Github shows a few repositories related to reading and plotting TOA5 Campbell data:

Thanks for your contribution!


Please log in or register to comment.