Summer Deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Creating functions
If statements
17. Passing in a data frame to a function
Summary

Instruction

So far, we've worked with passing in values and vectors to a function. As mentioned before, you can pass any object to a function that accepts an appropriate argument.

Take a look at this function definition:

extract_rows <- function(df, column, value){
  return (df[!is.na(df[, column]) & df[, column] == value, ])
}

This function takes three arguments: df (the data frame name), column (the column name), and value. It extracts all rows from the given data frame for which the given column contains the specified value.

With df[, column], we retrieve a specific column from the data frame. So if we pass in the survey data frame and the string "city", R will resolve df[, column] to survey[, "city"] at run time. This actually selects the specific column from the data frame.

With df[, column] == value, we obtain a logical vector of TRUE/FALSE values corresponding to the rows for which the column has the given value.

If you call this function with the arguments survey, "city", and "Amsterdam", R will resolve df[, column] == value into survey[, "city"] == "Amsterdam" and produce a logical vector of TRUE/FALSE values.

With

df[!is.na(df[, column]) & df[, column] == value, ]
we select only those rows for which the value is not NA and is equal to the given value. In our example with the arguments of survey, "city", and "Amsterdam", R will return a sub data frame of all survey participants from Amsterdam.

Exercise

Extract information about all survey participants who love their jobs. You can use the extract_rows() function.

The information is stored in the love_your_job column (people who love their job have value of "Y").