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.