To create a function with mutate in R, you can use the dplyr package, which is a popular package for data manipulation.
Here's a basic example that creates a function using mutate:
main.r352 chars15 lines
In this example, we first create a data frame called my_data
with two columns (x
and y
). We then create a function called square_column
that takes two arguments: data
(the data frame to modify) and column
(the name of the column to square).
Inside the function, we use !!
and sym()
to generate the column name dynamically, and use the ^
operator to square the column. Finally, we return the modified data frame.
To apply the function, we call square_column
and pass in my_data
and "y"
as arguments. This produces a new data frame called my_data_squared
where the y
column has been squared.
gistlibby LogSnag