AG真人百家乐官方网站

Skip to main content
NSF NEON, Operated by Battelle

Main navigation

  • AG真人百家乐官方网站 Us
    • Overview
      • Spatial and Temporal Design
      • History
    • Vision and Management
    • Advisory Groups
      • Science, Technology & Education Advisory Committee
      • Technical Working Groups (TWGs)
    • FAQ
    • Contact Us
      • Contact NEON Biorepository
      • Field Offices
    • User Accounts
    • Staff
    • Code of Conduct

    AG真人百家乐官方网站 Us

  • Data & Samples
    • Data Portal
      • Spatial Data & Maps
    • Data Themes
      • Biogeochemistry
      • Ecohydrology
      • Land Cover and Processes
      • Organisms, Populations, and Communities
    • Samples & Specimens
      • Discover and Use NEON Samples
        • Sample Types
        • Sample Repositories
        • Megapit and Distributed Initial Characterization Soil Archives
      • Sample Processing
      • Sample Quality
    • Collection Methods
      • Protocols & Standardized Methods
      • Airborne Remote Sensing
        • Flight Box Design
        • Flight Schedules and Coverage
        • Daily Flight Reports
          • AOP Flight Report Sign Up
        • Camera
        • Imaging Spectrometer
        • Lidar
      • Automated Instruments
        • Site Level Sampling Design
        • Sensor Collection Frequency
        • Instrumented Collection Types
          • Meteorology
          • Phenocams
          • Soil Sensors
          • Ground Water
          • Surface Water
      • Observational Sampling
        • Site Level Sampling Design
        • Sampling Schedules
        • Observation Types
          • Aquatic Organisms
            • Aquatic Microbes
            • Fish
            • Macroinvertebrates & Zooplankton
            • Periphyton, Phytoplankton, and Aquatic Plants
          • Terrestrial Organisms
            • Birds
            • Ground Beetles
            • Mosquitoes
            • Small Mammals
            • Soil Microbes
            • Terrestrial Plants
            • Ticks
          • Hydrology & Geomorphology
            • Discharge
            • Geomorphology
          • Biogeochemistry
          • DNA Sequences
          • Pathogens
          • Sediments
          • Soils
            • Soil Descriptions
        • Optimizing the Observational Sampling Designs
    • Data Notifications
    • Data Guidelines and Policies
      • Acknowledging and Citing NEON
      • Publishing Research Outputs
      • Usage Policies
    • Data Management
      • Data Availability
      • Data Formats and Conventions
      • Data Processing
      • Data Quality
      • Data Product Bundles
      • Data Product Revisions and Releases
        • Release 2021
        • Release 2022
        • Release 2023
        • Release 2024
        • Release-2025
      • NEON and Google
      • Externally Hosted Data

    Data & Samples

  • Field Sites
    • AG真人百家乐官方网站 Field Sites and Domains
    • Explore Field Sites

    Field Sites

  • Impact
    • Observatory Blog
    • Case Studies
    • Papers & Publications
    • Newsroom
      • NEON in the News
      • Newsletter Archive
      • Newsletter Sign Up

    Impact

  • Resources
    • Getting Started with NEON Data & Resources
    • Documents and Communication Resources
      • Papers & Publications
      • Outreach Materials
    • Code Hub
      • Code Resources Guidelines
      • Code Resources Submission
    • Learning Hub
      • Science Videos
      • Tutorials
      • Workshops & Courses
      • Teaching Modules
    • Research Support Services
      • Field Site Coordination
      • Letters of Support
      • Mobile Deployment Platforms
      • Permits and Permissions
      • AOP Flight Campaigns
      • Research Support FAQs
      • Research Support Projects
    • Funding Opportunities

    Resources

  • Get Involved
    • Advisory Groups
      • Science, Technology & Education Advisory Committee
      • Technical Working Groups
    • Upcoming Events
    • NEON Ambassador Program
      • Exploring NEON-Derived Data Products Workshop Series
    • Research and Collaborations
      • Environmental Data Science Innovation and Inclusion Lab
      • Collaboration with DOE BER User Facilities and Programs
      • EFI-NEON Ecological Forecasting Challenge
      • NEON Great Lakes User Group
      • NEON Science Summit
      • NCAR-NEON-Community Collaborations
        • NCAR-NEON Community Steering Committee
    • Community Engagement
      • How Community Feedback Impacts NEON Operations
    • Science Seminars and Data Skills Webinars
      • Past Years
    • Work Opportunities
      • Careers
      • Seasonal Fieldwork
      • Internships
        • Intern Alumni
    • Partners

    Get Involved

  • My Account
  • Search

Search

Learning Hub

  • Science Videos
  • Tutorials
  • Workshops & Courses
  • Teaching Modules

Breadcrumb

  1. Resources
  2. Learning Hub
  3. Tutorials
  4. Build & Work With Functions in R

Tutorial

Build & Work With Functions in R

Authors: Leah A. Wasser - Adapted from Software Carpentry

Last Updated: Apr 8, 2021

Sometimes we want to perform a calculation, or a set of calculations, multiple times in our code. We could write out the equation over and over in our code -- OR -- we could chose to build a function that allows us to repeat several operations with a single command. This tutorial will focus on creating functions in R.

Learning Objectives

After completing this tutorial, you will be able to:

  • Explain why we should divide programs into small, single-purpose functions.
  • Use a function that takes parameters (input values).
  • Return a value from a function.
  • Set default values for function parameters.
  • Write, or define, a function.
  • Test and debug a function. (This section in construction).

Things You鈥檒l Need To Complete This Tutorial

You will need the most current version of R and, preferably, RStudio loaded on your computer to complete this tutorial.


Set Working Directory: This lesson assumes that you have set your working directory to the location of the downloaded and unzipped data subsets.

An overview of setting the working directory in R can be found here.

R Script & Challenge Code: NEON data lessons often contain challenges that reinforce learned skills. If available, the code for challenge solutions is found in the downloadable R script of the entire lesson, available in the footer of each lesson page.

Creating Functions

Sometimes we want to perform a calculation, or a set of calculations, multiple times in our code. For example, we might need to convert units from Celsius to Kelvin, across multiple datasets and save if for future use.

We could write out the equation over and over in our code -- OR -- we could chose to build a function that allows us to repeat several operations with a single command. This tutorial will focus on creating functions in R.

Getting Started

Let's start by defining a function fahr_to_kelvin that converts temperature values from Fahrenheit to Kelvin:

fahr_to_kelvin <- function(temp) {
	kelvin <- ((temp - 32) * (5/9)) + 273.15
	kelvin
}

Notice the syntax used to define this function:

FunctionNameHere <- function(Input-variable-here){
	what-to-do-here
	what-to-return-here
}

The definition begins with the name of your new function. Use a good descriptor of the function you are doing and make sure it isn't the same as a a commonly used R function!

This is followed by the call to make it a function and a parenthesized list of parameter names. The parameters are the input values that the function will use to perform any calculations. In the case of fahr_to_kelvin, the input will be the temperature value that we wish to convert from fahrenheit to kelvin. You can have as many input parameters as you would like (but too many are poor style).

The body, or implementation, is surrounded by curly braces { }. Leaving the initial curly bracket at the end of the first line and the final one on its own line makes functions easier to read (for the human, the machine doesn't care). In many languages, the body of the function - the statements that are executed when it runs - must be indented, typically using 4 spaces.

**Data Tip:** While it is not mandatory in R to indent your code 4 spaces within a function, it is strongly recommended as good practice!

When we call the function, the values we pass to it are assigned to those variables so that we can use them inside the function.

The last line within the function is what R will evaluate as a returning value. Remember that the last line has to be a command that will print to the screen, and not an object definition, otherwise the function will return nothing - it will work, but will provide no output. In our example we print the value of the object Kelvin.

Calling our own function is no different from calling any other built in R function that you are familiar with. Let's try running our function.

# call function for F=32 degrees
fahr_to_kelvin(32)

## [1] 273.15

# We could use `paste()` to create a sentence with the answer
paste('The boiling point of water (212 Fahrenheit) is', 
      fahr_to_kelvin(212),
      'degrees Kelvin.')

## [1] "The boiling point of water (212 Fahrenheit) is 373.15 degrees Kelvin."

We've successfully called the function that we defined, and we have access to the value that we returned.

Question: What would happen if we instead wrote our function as:

fahr_to_kelvin_test <- function(temp) {
	kelvin <- ((temp - 32) * (5 / 9)) + 273.15
}

Try it:

fahr_to_kelvin_test(32)

Nothing is returned! This is because we didn't specify what the output was in the final line of the function.

However, we can see that the function still worked by assigning the function to object "a" and calling "a".

# assign to a
a <- fahr_to_kelvin_test(32)

# value of a
a

## [1] 273.15

We can see that even though there was no output from the function, the function was still operational.

###Variable Scope

In R, variables assigned a value within a function do not retain their values outside of the function.

x <- 1:3
x

## [1] 1 2 3

# define a function to add 1 to the temporary variable 'input'
plus_one <- function(input) {
  input <- input + 1
}

# run our function
plus_one(x)

# x has not actually changed outside of the function
x

## [1] 1 2 3

To change a variable outside of a function you must assign the funciton's output to that variable.

plus_one <- function(input) {
  output <- input + 1     # store results to output variable
  output                  # return output variable
}

# assign the results of our function to x
x <- plus_one(x)
x

## [1] 2 3 4
### Challenge: Writing Functions

Now that we've seen how to turn Fahrenheit into Kelvin, try your hand at converting Kelvin to Celsius. Remember, for the same temperature Kelvin is 273.15 degrees less than Celsius.

Compound Functions

What about converting Fahrenheit to Celsius? We could write out the formula as a new function or we can combine the two functions we have already created. It might seem a bit silly to do this just for converting from Fahrenheit to Celcius but think about the other applications where you will use functions!

# use two functions (F->K & K->C) to create a new one (F->C)
fahr_to_celsius <- function(temp) {
	temp_k <- fahr_to_kelvin(temp)
	temp_c <- kelvin_to_celsius(temp_k)
	temp_c
}
	
paste('freezing point of water (32 Fahrenheit) in Celsius:', 
      fahr_to_celsius(32.0))

## [1] "freezing point of water (32 Fahrenheit) in Celsius: 0"

This is our first taste of how larger programs are built: we define basic operations, then combine them in ever-large chunks to get the effect we want. Real-life functions will usually be larger than the ones shown here鈥攖ypically half a dozen to a few dozen lines鈥攂ut they shouldn't ever be much longer than that, or the next person who reads it won't be able to understand what's going on.

Get Lesson Code

R-Basics-Of-Functions.R

Questions?

If you have questions or comments on this content, please contact us.

Contact Us
NSF NEON, Operated by Battelle

Follow Us:

Join Our Newsletter

Get updates on events, opportunities, and how NEON is being used today.

Subscribe Now

Footer

  • AG真人百家乐官方网站 Us
  • Newsroom
  • Contact Us
  • Terms & Conditions
  • Careers
  • Code of Conduct

Copyright © Battelle, 2025

The National Ecological Observatory Network is a major facility fully funded by the U.S. National Science Foundation.

Any opinions, findings and conclusions or recommendations expressed in this material do not necessarily reflect the views of the U.S. National Science Foundation.