Author Archives: Rumana

Snow-Day Make-up Assignment on DataCamp

Name: Rumana Hassin Syed

Course: Writing Functions in R.

I chose to do this course by trial and error. I saw the list given on the openlab page. I discarded the idea of doing General programming in R right away because I wanted to do something slightly more difficult. So I looked into other options like Making Reports using R. But soon realized I do not know R enough to do it. So I looked back into the general programming option and looked up the videos available for Intermediate R, Writing Functions in R and Data Visualization. For intermediate R, I thought it was very similar to what we have already done as homework during the semester, just the next level of it. I liked the concept of data visualization too, but I chose to do Writing Functions in R because I thought if functions are said to be the building blocks of R, I better start with the fundamentals and then go step by step from there to get better in R language.

Overall description of the course:

In this course I learnt that like most other programming languages I have been taught in my major classes like C/C++, Java, the fundamental building block for R language is also the Functions. And through this course I got to know more about these fundamental building blocks of R that will eventually help me make my code more readable, avoid coding errors, and automate repetitive tasks.

Following are the topics covered in the course

  • A quick refresher:  As the name suggest it was indeed a quick recollection of what we already know. This section of course reviews some of the basic thing like syntax and data types in R. Not only that it also introduced new concepts like writing loops and subsetting in R, that I was not aware of before.
  • When and how you should write a function: Again as the name suggests, this section of the course teaches you when and how to write functions in R efficiently.  From here I learnt that I should understand it is better to use a function when I have already copied and pasted a piece of code at least twice. This will not only save my time, but also reduces the chances of making silly errors and any update to the code could be made easily, i.e, if I have to make small change to the code, I have to make it only once unlike the other method where I have to change it as many times as I have copied and pasted.
  • Functional programming: In this sectional I learnt what is functional programming and why do we use it at all. I learnt how to use loop to avoid redundancy. Thereafter also learnt how to convert that loop into a function.
  • Advanced inputs and outputs: In this section of the course we deal with failure. I learnt to write safe functions in R. I learnt about functions that help with failure cases. These are listed below:
    • safely() captures the successful result or the error, always returns a list
    • possible() always succeeds, you give it a default value to return when there is an error
    • quietly() captures printed output, messages, and warnings instead of capturing errors
  • Robust functions: In this section, I learnt about functions whose output was based on the input. Some of them are as follows:
    • df[, vars]
    • subset(df, x == y)
    • data.frame(x = "a")

Description of one topic:

For the detailed description of one topic, I choose to describe the Quick Refresher. Although I found the entire course to be really useful, I thought without this first section, where I got the heads up of what to expect and also reviewed some general topics, the entire course would be meaningless. It would only be a struggle to hunt for answers without understanding a thing. Thus I choose to elaborate more on it.

In the Quick Refresher the following topics were covered:

  • Writing a function
  • Arguments
  • Function output
  • Environments
  • Testing your understanding of scoping (1)
  • Testing your understanding of scoping (2)
  • Testing your understanding of scoping (3)
  • Data structures
  • Subsetting lists
  • Exploring lists
  • for loops

From this part I came to know that functions can be treated like usual R objects. I reviewed that a function is made up of three distinct parts, viz., Argument, the Body and the Environment. I knew about argument and the body but was not sure what is meant by the environment, which I learnt here for the first time. Following are some of the aspects of an environment:

  1. When a function is called a new environment is made for the function to do its work
  2. This new environment is populated with the argument values
  3. Objects are looked for first in this environment
  4. If they are not found they are looked for in the environment that the function was created in

I also learnt that Return value is the last evaluated expression or the first evaluated return() expression. This section also gave me a great review on Data Structures. I got to know The key properties of vectors are its type and the length and there are only two types of vectors in general: Atomic Vectors and Lists. However, Atomic Vectors is subdivided into 6 types namely logical, integer, double, character, complex, raw and List is also called a recursive vector because lists can contain other lists. Lists are very useful because they can contain heterogeneous objects. 

Although I often got stuck and had to get some help by looking things up, I admit that the entire course was worth taking and will be very useful in the long run.