This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "An introduction to using functions in R" | |
author: "Cameron M. Nugent" | |
date: "2019-10-17" | |
--- | |
## Introduction | |
R is considered to be a functional programming language. What this means is that the syntax and rules of the language are most effective when you write code built around the use of functions. They allow you to modularize code, thereby isolating different blocks in a way that makes your code more generalized, reuseable, readable and easier to debug. This post is meant to introduce the three basic parts of a function (the arguments, the body and the environment), demonstrate some of the rules that govern function behaviour and provide a launchpad to help you get started writing functions in your own R code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for a recent tutorial at the university of guelph R users group, I was going through | |
# how to generate summary stats & tidy dataframes from messy data sources | |
# we were working with text data, and the exercise I designed called for us to process | |
# a series of sentences and answer 3 questions about each line: | |
# is the line dialouge (by presence of a quotation mark) | |
# is the line a question (by presence of a question mark) and | |
# what is the word count of the line |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#original undecorated | |
class example_class: | |
def __init__(self, *, kw1 = None, kw2 = None): | |
if kw1 is not None or kw2 is not None: | |
#do stuff with the args | |
self.kw1 = kw1 | |
self.kw2 = kw2 | |
def read(self, *, kw1 = None, kw2 = None): | |
if kw1 is not None or kw2 is not None: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ORIGINAL version | |
def merge_sorted(list_a, list_b): | |
""" take two lists of integers sorted smallest to largest, | |
merge them to a single output list with | |
no duplicate values """ | |
output_list = [] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from concurrent import futures | |
import pandas as pd | |
from pandas import DataFrame | |
import pandas_datareader.data as web | |
def download_stock(stock): | |
""" try to query the iex for a stock, if failed note with print """ | |
try: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import pandas as pd | |
from pandas import DataFrame | |
import pandas_datareader.data as web | |
def download_stock(stock): | |
""" try to query the iex for a stock, if failed note with print """ | |
try: | |
print(stock) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
NewerOlder