# Basic Logical Expressions and Curly Brackets in R – Baiting

In the past two weeks, I went through more tutorials and learned how to select, sort, and rank columns in a matrix or a data frame. However, I am more thrilled to talk about the “if-else” logic expression as I noticed something strange while trying out myself. If you are interested in studying this topic yourself, you may find this edx video tutorial and this website to be helpful. They are both free.

The first thing I entered was: Code 1

This is the simplest “if-else” expression. The first line means set “a” as a variable and a = 2. The third to the fifth line is the logic expression. If “a” doesn’t equal to 0 (in R, “!=” means “≠”）, the output would be 1/a. Otherwise (if a = 0), the output should be “FALSE”. This is what happens when we run these lines. Output 1

Obviously, the logic works. We have a = 2, so a ≠ 0. This matches with the condition that the result = 1/a = 1/2 = 0.5. However, if we start “else” in a new line, then something strange will happen. Code 2

When we start “else” in a new line, the R Studio says there is an error. If we run the codes, R Console would show the following: Output 2

This triggers my curiosity, since this is the only time that starting a new line would lead to different results. Let me explain it quickly using another example. Code 3

In R, the two sections in Code 3 mean exactly the same thing. The only difference is the first section occupies 1 line while the second section occupies 4. They both mean read the document “oasis_cross-sectional.csv” as a data frame and name the data frame as “A”. This example is not an exception. In fact, starting codes from a new line work in most expressions I learned. However, it doesn’t work in the “if-else” expression. To figure out the answer, I did some research on the curly bracket “{” and “}” in R.

According to “R in a Nutshell, 2nd Edition” by Joseph Adler, curly brackets (braces) are “used to evaluate a series of expression” or “used to group a set of operations in the body of a function” (Adler). This hints that the curly brackets only work when there is another corresponding expression, like “if-else. ” If there is no such expressions, curly brackets would have no effects.

Why this matters? Obviously, curly brackets only work when you have pairs of heads and tails. The computer only reads the statement in between. Here is an example: Code 4 Code 5

If you compare Code 4 and Code 5, you would notice Code 4 is missing the tail curly bracket ( } ). So, your computer would read Code 4 as a bunch of characters and Code 5 as a statement.

Again, curly brackets need to work with another expression. My hypothesis is the head bracket would find the first expression in front of itself and match with it. Then, the corresponding tail bracket will close the statement. This explains why Code 1 works when we have started the head bracket in a new line. The bracket would automatically find the “if” expression and match with it.

However, the “else” expression doesn’t do the same work. When you start “else” in a new line, it wouldn’t find the corresponding “if.” I believe this is not a flaw, but an intentional design. A head bracket always needs another expression, but “else” doesn’t always need “if”, it may match with other expressions, like “else if”. So letting “else” find its own expression would lead to many logic errors.

To test my conclusion, I used the following codes: Code 6

In Code 6, the highlighted lines are the ones I inserted intentionally for testing. According to our hypothesis, the first head curly bracket in line 9 would find the statement in front of it. This means line 7 would match with line 9. At the same time, the “if” expression in line 3 would match with line 5, as after we give the condition (a > 6), it automatically considers line 5 as the “so what.” This is what happens when we run the codes: Output 6

Only the first line works! The logic function “if-else” fails as the brackets can no longer find the correct expression. In comparison to Code 2 and Output 2, even though R identify both errors as “unexpected ‘else’,” it is obvious that the problem with Code 2 is the tail bracket while the problem with Code 6 is the head bracket. In addition, line 7 fails to work as line 5. Line 5, the line that is supposed to match with line 7, is now matched with line 3 (as shown by the “+” sign in the output). This means we no longer have a definition for A, so we can’t find it.

This is the end of my main topic. In this blog, we successfully found a problem and established some reasonable conclusions. Of course, these conclusions may be wrong, but the process of identifying and solving a problem was fun for me! When codes fail to run, I believe it is always important to read the error notice to identify the issue. Furthermore, it is also helpful to reflect on and identify the deeper reason that leads to the error.

Works Cited

Adler, Joseph. R in a Nutshell. 2nd ed., Sebastopol, O’Reilly Media, 2012. O’REILLY, http://www.oreilly.com/library/view/r-in-a/9781449358204. Accessed 29 Apr. 2019.

“ifelse.” RDocumentation, www.rdocumentation.org/packages/base/versions/3.5.3/topics/ifelse. Accessed 29 Apr. 2019.

Irizarry, Rafael. “Basic Conditionals.” Edx.org. Lecture.

# R (and my plan) – Baiting https://www.coursera.org/learn/r-programming Continue reading

# RSA Algorithm – Baiting https://www.boxcryptor.com/en/encryption/

In this blog, I would like to provide updates on the M3 Challenge and my research project in Abstract Algebra for Chester County Science Fair. Then, I will introduce the RSA Algorithm, the most used information encryption algorithm in the world and how it relates to my project.

Continue reading

# Last Two Weeks – Baiting https://www.mathworks.com/academia/student-competitions/mathworks-math-modeling-challenge.html

Since my last blog, I have been working on three different projects: Modeling the Future, Science Fair (Abstract Algebra), and M3 Challenge, another mathematical modeling competition. In this blog, I will talk about all three competitions I have been working on. For Abstract Algebra, I will also use part of my paper to talk about Lagrange’s Theorem. Continue reading

# How I Redefine Addition – Baiting Addition: https://www.tes.com/lessons/ZeGyGhKptzKMpA/addition

In my last blog, I talked about my plan for the semester, why I want to study abstract algebra, and some basic concepts involving mapping. In this blog, I will first provide some update on Modeling the Future, a team math competition we have been working on, and how I redefine addition in group theory. I will also talk about my plan for the science fair. Continue reading

# Abstract Algebra: A NEW Start – Baiting Retrieved from https://www.booktopia.com.au/abstract-algebra-gerhard-rosenberger/prod9783110250084.html

In this blog, I would like to briefly review my Fall semester and share my plan for the Spring.

In the past four months, I worked on Differential Equations and its related topics. Through following the MIT Open Course Ware, I learned different methods to solve DEs and their implications in real life.

Continue reading

# The Fourier Series: An Introduction – Baiting The Fourier Series

In the past few weeks and Thanksgiving break, I finished Unit II and moved on to Unit III. In this unit, I will primarily focus on the Fourier Series, Laplace Transformation, their connections with Differential Equations, and their applications in reality. In this blog, I will introduce the Fourier Series of periodic functions, including the trigonometry functions, the sin and cos. Continue reading

# The Spring Model Continued – Baiting In the past two weeks, I worked on Second Order Differential Equations with constant coefficients and learned more about the Spring-Damper Model. In this blog, I will provide a brief recap of the basic knowledge, and then provide further analysis of the same model. If you are interested in my last blog, please visit here. Continue reading

# Euler’s Formula – Baiting As mentioned in my last post, I continued to develop my algebraic skills for solving differential equations during the past two weeks. In this blog, I want to introduce Euler’s Formula, the most impressive piece of math work I have ever seen. In specific, I will dive deep into the mathematical proof of this formula and explain its broad application.

Euler’s Formula consists of a simple line: In this equation, e represents the irrational constant 2.71828…, or more specifically , and i represents . With an irrational number and an imaginary number involved, Euler’s Formula is not easy to visualize in the regular coordinate system. In order to make this blog more comprehensive to everyone, I will offer an algebraic proof, so that we don’t have to learn the complex Cartesian and Polar coordinate system from the beginning.

To prove Euler’s Formula, we have to prove it satisfies these three fundamental properties:

1. (Law of exponential)
2. (Law and definition of constant e in calculus)
3. Taylor’s series of the left should equal to Taylor’s series on the right as the number of terms approaches infinite.

So let’s start from the first one! Remember we are now assuming Euler’s Formula is correct and testing if it [actually] follows all of the properties. After proving the Euler’s Formula follows exponential identity, we can move on to proving the identity of e.

The concept of constant e was first brought up by Euler himself,  and is sometimes referred to as “Euler’s Number.” The core definition of e is the rate of change always equal to its self. If you want to know more about this mysterious number, please visit here. Due to its ingenious definition, e is used in multiple areas including charging/discharging a capacitor or calculating quantities related to half-life.

Now here is a simple and straightforward proof for Part II. In the last part, we are proving the polynomials of e^iθ equals the polynomials of cos(x)+i*sin(x) from Taylor’s series. If you remember some relevant knowledge from Calculus II, this would also be straightforward. https://slideplayer.com/slide/10994153/

Now, we have proved the Euler’s Formula. And I will briefly and broadly cover its applications. If you are interested in knowing more about any of these topics, please tell me and I will discuss them in the future!

Trigonometry, Fourier transformation, Taylor’s series, …. the span of the impact of Euler’s Formula goes on and on. With Euler’s Formula, we can easily model rotations in complex coordinate system or even explain the spiral movements of the starts; with Euler’s Formula, the trigonometry relationships become easier than ever; and most importantly, with Euler’s Formula, we can, for the first time in history, cross the wall between transcendental numbers and algebraic numbers.

Euler’s Formula is a piece of math theorem, but also a piece of art. Like Mona Lisa’s Smile by Leonardo da Vinci, Euler’s Formula contains much more than it seems. When rewritten as e^(iπ)+1=0, the simple line involves plus/minus, multiplication/division, exponential, trigonometry, complex number, the concept of zero, and transcendental numbers. Even within itself, there is a sea of knowledge waiting for me to explore.

To me, Euler’s Formula is just a beginning. On my journey of learning, more fascinating math topics will be closely studied and researched. What I will not forget, however, is the excitement after seeing this beautiful math work. Like a line of a poem that eulogizes our life, Euler’s Formula thoroughly depicts the beauty of math.

References

Fourier Transforms [Illustration]. (2010, Fall). Retrieved from https://slideplayer.com/slide/10994153/

Arthur Mattuck, Haynes Miller, Jeremy Orloff, and John Lewis. 18.03SC Differential Equations. Fall 2011. Massachusetts Institute of Technology: MIT OpenCourseWare, https://ocw.mit.edu. License: Creative Commons BY-NC-SA.

Images:

Leonhard Euler [Photograph]. (n.d.). Retrieved from https://www.usna.edu/Users/math/meh/euler.html

O’Connor, J. J., & Robertson, E. F. (2001, September). The number e. Retrieved October 8, 2018, from MacTutor History of Mathematics archive website: http://www-history.mcs.st-and.ac.uk/HistTopics/e.html