# Interactive code templates in Data Refinery #

Data Refinery provides interactive templates for you to code operations, functions, and logical operators\. Access the templates from the command\-line text box at the top of the page\. The templates include interactive assistance to help you with the syntax options\.

Important: Support is for the operations and functions in the user interface\. If you insert other operations or functions from an open source library, the Data Refinery flow might fail\. See the command\-line help and be sure to use the list of operations or functions from the templates\. Use the examples in the templates to further customize the syntax as needed\.

<!-- <ul> -->

 *  [Operations](https://dataplatform.cloud.ibm.com/docs/content/wsj/refinery/code_operations.html?context=cdpaas&locale=en#operations)
 *  [Functions](https://dataplatform.cloud.ibm.com/docs/content/wsj/refinery/code_operations.html?context=cdpaas&locale=en#functions)
 *  [Logical operators](https://dataplatform.cloud.ibm.com/docs/content/wsj/refinery/code_operations.html?context=cdpaas&locale=en#logical_operators)

<!-- </ul> -->

## Operations ##

### arrange ###

arrange(\``<column>`\`)  
Sort rows, in ascending order, by the specified columns\.  

arrange(desc(\``<column>`\`))  
Sort rows, in descending order, by the specified column\.  

arrange(\``<column>`\`, \``<column>`\`)  
Sort rows, in ascending order, by each specified, successive column, keeping the order from the prior sort intact\.

### count ###

count()  
Total the data by group\.

count(\``<column>`\`)  
Group the data by the specified column and return the number of rows with unique values (for string values) or return the total for each group (for numeric values)\.

count(\``<column>`\`, wt=\``<column>`\`)  
Group the data by the specified column and return the number of rows with unique values (for string values) or return the total for each group (for numeric values) in the specified weight column\.

count(\``<column>`\`, wt=`<func>`(\``<column>`\`))  
Group the data by the specified column and return the result of the function applied to the specified weight column\.

count(\``<column>`\`, wt=`<func>`(\``<column>`\`), sort = `<logical>`)  
Group the data by the specified column and return the result of the function applied to the specified weight column, sorted or not\.

### distinct ###

distinct()  
Keep distinct, unique rows based on all columns or on specified columns\.

### filter ###

filter(\``<column>`\` `<logicalOperator>` provide\_value)  
Keep rows that meet the specified condition and filter out all other rows\.  
For the Boolean column type, provide\_value should be uppercase TRUE or FALSE\.

filter(\``<column>`\`== `<logical>`)  
Keep rows that meet the specified filter conditions based on logical value TRUE or FALSE\.

filter(`<func>`(\``<column>`\`) `<logicalOperator>` provide\_value)  
Keep rows that meet the specified condition and filter out all other rows\. The condition can apply a function to a column on the left side of the operator\.

filter(\``<column>`\` `<logicalOperator>``<func(column)>`)  
Keep rows that meet the specified condition and filter out all other rows\. The condition can apply a function to a column on the right side of the operator\.

filter(`<logicalfunc(column)>`)  
Keep rows that meet the specified condition and filter out all other rows\. The condition can apply a logical function to a column\.

filter(\``<column>`\` `<logicalOperator>` provide\_value `<andor>` \``<column>`\` `<logicalOperator>` provide\_value )  
Keep rows that meet the specified conditions and filter out all other rows\.

### group\_by ###

group\_by(\``<column>`\`)  
Group the data based on the specified column\.

group\_by(desc(\``<column>`\`))  
Group the data, in descending order, based on the specified column\.

### mutate ###

mutate(provide\_new\_column = \``<column>`\`)  
Add a new column and keep existing columns\.

mutate(provide\_new\_column = `<func(column)>`)  
Add a new column by using the specified expression, which applies a function to a column\. Keep existing columns\.

mutate(provide\_new\_column = case\_when(\``<column>`\` `<operator>` provide\_value\_or\_column\_to\_compare ~ provide\_value\_or\_column\_to\_replace, \``<column>`\` `<operator>` provide\_value\_or\_column\_to\_compare ~ provide\_value\_or\_column\_to\_replace, TRUE ~ provide\_default\_value\_or\_column))  
Add a new column by using the specified conditional expression\.

mutate(provide\_new\_column = \``<column>`\` `<operator>` \``<column>`\`)  
Add a new column by using the specified expression, which performs a calculation with existing columns\. Keep existing columns\.

mutate(provide\_new\_column = coalesce(\``<column>`\`, \``<column>`\`))  
Add a new column by using the specified expression, which replaces missing values in the new column with values from another, specified column\. As an alternative to specifying another column, you can specify a value, a function on a column, or a function on a value\. Keep existing columns\.

mutate(provide\_new\_column = if\_else(\``<column>`\` `<logicalOperator>` provide\_value, provide\_value\_for\_true, provide\_value\_for\_false))  
Add a new column by using the specified conditional expression\. Keep existing columns\.

mutate(provide\_new\_column = \``<column>`\`, provide\_new\_column = \``<column>`\`)  
Add multiple new columns and keep existing columns\.

mutate(provide\_new\_column = n())  
Count the values in the groups\. Ensure grouping is done already using group\_by\. Keep existing columns\.

### mutate\_all ###

mutate\_all(funs(`<func>`))  
Apply the specified function to all of the columns and overwrite the existing values in those columns\. Specify whether to remove missing values\.

mutate\_all(funs(\. `<operator>` provide\_value))  
Apply the specified operator to all of the columns and overwrite the existing values in those columns\.

mutate\_all(funs("provide\_value" = \. `<operator>` provide\_value))  
Apply the specified operator to all of the columns and create new columns to hold the results\. Give the new columns names that end with the specified value\.

### mutate\_at ###

mutate\_at(vars(\``<column>`\`), funs(`<func>`))  
Apply functions to the specified columns\.

### mutate\_if ###

mutate\_if(`<predicateFunc>`, `<func>`)  
Apply functions to the columns that meet the specified condition\.

mutate\_if(`<predicateFunc>`, funs( \. `<operator>` provide\_value))  
Apply the specified operator to the columns that meet the specified condition\.

mutate\_if(`<predicateFunc>`, funs(`<func>`))  
Apply functions to the columns that meet the specified condition\. Specify whether to remove missing values\.

### rename ###

rename(provide\_new\_column = \``<column>`\`)  
Rename the specified column\.

### sample\_frac ###

sample\_frac(provide\_number\_between\_0\_and\_1, weight=\``<column>`\`,replace=`<logical>`)  
Generate a random sample based on a percentage of the data\. weight is optional and is the ratio of probability the row will be chosen\. Provide a numeric column\. replace is optional and its Default is FALSE\.

### sample\_n ###

sample\_n(provide\_number\_of\_rows,weight=\``<column>`\`,replace=`<logical>`)  
Generate a random sample of data based on a number of rows\. weight is optional and is the ratio of probability the row will be chosen\. Provide a numeric column\. replace is optional and its default is FALSE\.

### select ###

select(\``<column>`\`)  
Keep the specified column\.

select(\-\``<column>`\`)  
Remove the specified column\.

select(starts\_with("provide\_text\_value"))  
Keep columns with names that start with the specified value\.

select(ends\_with("provide\_text\_value"))  
Keep columns with names that end with the specified value\.

select(contains("provide\_text\_value"))  
Keep columns with names that contain the specified value\.

select(matches ("provide\_text\_value"))  
Keep columns with names that match the specified value\. The specified value can be text or a regular expression\.

select(\``<column>`\`:\``<column>`\`)  
Keep the columns in the specified range\. Specify the range as from one column to another column\.

select(\``<column>`\`, everything())  
Keep all of the columns, but make the specified column the first column\.

select(\``<column>`\`, \``<column>`\`)  
Keep the specified columns\.

### select\_if ###

select\_if(`<predicateFunc>`) Keep columns that meet the specified condition\. Supported functions include:

<!-- <ul> -->

 *  contains
 *  ends\_with
 *  matches
 *  num\_range
 *  starts\_with

<!-- </ul> -->

### summarize ###

summarize(provide\_new\_column = `<func>`(\``<column>`\`))  
Apply aggregate functions to the specified columns to reduce multiple column values to a single value\. Be sure to group the column data first by using the group\_by operation\.

### summarize\_all ###

summarize\_all(`<func>`)  
Apply an aggregate function to all of the columns to reduce multiple column values to a single value\. Specify whether to remove missing values\. Be sure to group the column data first by using the group\_by operation\.

summarize\_all(funs(`<func>`))  
Apply multiple aggregate functions to all of the columns to reduce multiple column values to a single value\. Create new columns to hold the results\. Specify whether to remove missing values\. Be sure to group the column data first by using the group\_by operation\.

### summarize\_if ###

summarize\_if(`<predicate_conditions>`,\.\.\.)  
Apply aggregate functions to columns that meet the specified conditions to reduce multiple column values to a single value\. Specify whether to remove missing values\. Be sure to group the column data first by using the group\_by operation\. Supported functions include:

<!-- <ul> -->

 *  count
 *  max
 *  mean
 *  min
 *  standard deviation
 *  sum

<!-- </ul> -->

### tally ###

tally()  
Counts the number of rows (for string columns) or totals the data (for numeric values) by group\. Be sure to group the column data first by using the group\_by operation\.

tally(wt=\``<column>`\`)  
Counts the number of rows (for string columns) or totals the data (for numeric columns) by group for the weighted column\.

tally( wt=`<func>`(\``<column>`\`), sort = `<logical>`)  
Applies a function to the specified weighted column and returns the result, by group, sorted or not\.

### top\_n ###

top\_n(provide\_value)  
Select the top or bottom N rows (by value) in each group\. Specify a positive integer to select the top N rows; specify a negative integer to select the bottom N rows\.

top\_n(provide\_value, \``<column>`\`)  
Select the top or bottom N rows (by value) in each group, based on the specified column\. Specify a positive integer to select the top N rows; specify a negative integer to select the bottom N rows\.

If duplicate rows affect the count, use the **Remove duplicates** GUI operation prior to using the top\_n() operation\.

### transmute ###

transmute(`<new_or_existing_column>` = \``<column>`\`)  
Add a new column or overwrite an existing one by using the specified expression\. Keep only columns that are mentioned in the expression\.

transmute(`<new_or_existing_column>` = `<func(column)>`)  
Add a new column or overwrite an existing one by applying a function to the specified column\. Keep only columns that are mentioned in the expression\.

transmute(`<new_or_existing_column>` = \``<column>`\` `<operator>` \``<column>`\`)  
Add a new column or overwrite an existing one by applying an operator to the specified column\. Keep only columns that are mentioned in the expression\.

transmute(`<new_or_existing_column>` = \``<column>`\`, `<new_or_existing_column>` = \``<column>`\`)  
Add multiple new columns\. Keep only columns that are mentioned in the expression\.

transmute(`<new_or_existing_column>` = if\_else( provide\_value, provide\_value\_for\_true, provide\_value\_for\_false))  
Add a new column or overwrite an existing one by using the specified conditional expressions\. Keep only columns that are mentioned in the expressions\.

### ungroup ###

ungroup()  
Ungroup the data\.

## Functions ##

### Aggregate ###

<!-- <ul> -->

 *  mean
 *  min
 *  n
 *  sd
 *  sum

<!-- </ul> -->

### Logical ###

<!-- <ul> -->

 *  is\.na

<!-- </ul> -->

### Numerical ###

<!-- <ul> -->

 *  abs
 *  coalesce
 *  cut
 *  exp
 *  floor

<!-- </ul> -->

### Text ###

<!-- <ul> -->

 *  c
 *  coalesce
 *  paste
 *  tolower
 *  toupper

<!-- </ul> -->

### Type ###

<!-- <ul> -->

 *  as\.character
 *  as\.double
 *  as\.integer
 *  as\.logical

<!-- </ul> -->

## Logical operators ##

<!-- <ul> -->

 *  <  
    
 *  <=  
    
 *  >=  
    
 *  >  
    
 *  between  
    
 *  \!=  
    
 *  ==  
    
 *  %in%  
    
<!-- </ul> -->

**Parent topic:**[Refining data](https://dataplatform.cloud.ibm.com/docs/content/wsj/refinery/refining_data.html)

<!-- </article "role="article" "> -->