lecture 2: formulas & functions

37
MTH4114: Computing and Data Analysis with Excel Lecture 2: Formulas & Functions 1

Upload: others

Post on 20-Dec-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 2: Formulas & Functions

MTH4114: Computing and Data Analysis with Excel

Lecture 2: Formulas & Functions

1

Page 2: Lecture 2: Formulas & Functions

Labs

Labs are

• Thursday 9-11 (Online and On Campus)

• Friday 12-2 (On Campus only)

• Friday 2-4 (On Campus only)

Please come to your assigned lab!!

2

Page 3: Lecture 2: Formulas & Functions

Labs (prep work)

Preparation needed for Labs

• Review the lecture content

• Read the exercises

• You do not need to do the exercises before the lab (unlike your other modules)

Before the Labs this week:

• Make sure you can access Excel: either your own copy or appsanywhere

• Make sure you know which lab you are assigned to.

3

Page 4: Lecture 2: Formulas & Functions

AutoFill (end of Lecture 1)

AutoFill can automatically complete ‘sequences’ of data, e.g. to enter the first 40 days of the year into cells A1 to A40, you can just

• Type 01-Jan into cell A1 and 02-Jan into cell A2

• Use the mouse to select A1 and A2 together

• Drag the cursor from the bottom-right corner of A2 to A40

• Excel will try to guess what you mean when you try to use AutoFill, and after you have done the AutoFill you can use the AutoFill icon to select options

• AutoFill can also copy formulas: (but copy and paste is just as good in this case)

4

Page 5: Lecture 2: Formulas & Functions

Reminder: Entering formulas

Last week, we started to discuss formulas

Remember all formulas start with an equals sign =

There are several ways to enter a formula into a cell:

• Type the formula into the cell directly

• Select the cell and then type the formula into the formula bar

• Select the cell and then use the formula builder by clicking . This tool is also called the “Insert Function” tool.

• Click on the Formulas tab and use the various shortcuts

5

Page 6: Lecture 2: Formulas & Functions

Reminder: formulas

Examples:

• Mathematical functions, e.g. =SIN(0) (sine of 0), =COS(1) (cosine of 1), =LN(3) (natural logarithm of 3), etc.

• Functions on ranges: SUM, AVERAGE etc

This week, we

• Look at the formula builder in more detail, and

• discuss some important functions.

6

Page 7: Lecture 2: Formulas & Functions

Some examples

Sum: Average:

We see the basic syntax:

=FUNCTION(Arguments)

• '=' identifies the input as a function to be evaluated

• FUNCTION is the name of the function (above: SUM & AVERAGE)

• Arguments are the inputs to the function (above the values to be added / averaged)

7

Page 8: Lecture 2: Formulas & Functions

'=' identifies the command as a function:

Basic Syntax: function

8

Incorrect:Without the = the input is identified as text

Correct:The formula is identified and evaluated

Page 9: Lecture 2: Formulas & Functions

Basic Syntax: Arguments

Arguments are the input to a function, just like for mathematical functions

Arguments may be

• A reference to a single cell (e.g. A1)

• A reference to a range of cells (e.g. A1:A6)

• A manual input. E.g. numbers such as 1.5, or text (which needs to be in quotes "Pass")

• The output of another function. That is you may nest functions. E.g., =SIN(COS(1))

9

Page 10: Lecture 2: Formulas & Functions

Basic Syntax: Arguments

• Multiple inputs, separated by a comma, can be given for some functions. For example

• '=SUM(3, 6, -7)' which will give 2

• '=SUM(A1:A6, 6)' will sum the values A1 to A6 and then add 6

• With multiple inputs, we can mix the different types:

10

Page 11: Lecture 2: Formulas & Functions

Basic Syntax: Errors

The behaviour for different types of arguments depends on the function

• #VALUE! is displayed, when the arguments don’t match the function (or a warning pops up)

• E.g. if a function expects a number but the argument is text.

11

Page 12: Lecture 2: Formulas & Functions

Basic Syntax: Errors

• If your formulas contain circular references (e.g. if you enter =D10 in cell D9 and =D9 in cell D10) you will see a warning like this one at the bottom of the screen:

You may also see an error box like the one below.

12

Page 13: Lecture 2: Formulas & Functions

Required Arguments

• Most functions have at least one required argument. The function cannot be calledwithout these arguments.E.g. calling '=SUM()':

• Some functions require two or morearguments. E.g., '=LEFT(text, number_of_characters)'

• A very small number of functions do not require an argument: they do still require empty parenthesis:E.g. '=PI()' which returns the value of 𝜋.

13

Page 14: Lecture 2: Formulas & Functions

Optional Arguments

Some functions have additional optional arguments. These arguments are not necessary but are used when provided

• Example: SUM(number1, number2, …)

• One input 'number1' is required, the remaining input numbers are optional. Note 'number' can be a single number or a range.

• '=SUM(A1:A6)' sums the values in the six cells A1 to A6

• '=SUM(A1:A6, C1:C6, E1:E6)' sums the values in the 18 provided cells

14

Page 15: Lecture 2: Formulas & Functions

Finding functions

How do you find Excel functions?

• ‘Formulas’ tab

• ‘Insert function’ tool

• Google

• We focus on the Excel tools, but Google (and stackoverflow.com) is always a good place to search for answers related to computing questions

15

Page 16: Lecture 2: Formulas & Functions

Insert Function tool

Insert Function tool

• Displays all functions available

• Grouped by most recently used, or by category

To insert a function:

• Click left of the formulabar

• Find a function and read description for its usage

• Click 'OK' to add function

• Insert arguments and pressenter to evaluate function

16

Page 17: Lecture 2: Formulas & Functions

Function dialog box

Function Arguments dialog box

• Helps you enter all of thecorrect values as functionarguments

• Each argument is listed,required ones are in bold

• Help info displayed for current argument text box

• Preview of data at right

• Results are instantly calculated and displayed if enough arguments entered

17

Page 18: Lecture 2: Formulas & Functions

Function dialog box

• Function arguments dialog box shrinks temporarily using collapse button. This is useful if you want to select a cell or range with the mouse.

• Restores to full size when done

18

Page 19: Lecture 2: Formulas & Functions

Formulas Tab

The ‘Formulas’ tab is a shortcut to functions grouped by their category:

Categories:

• Financial: Specialised financial computations, such as IRR (internal rate of return), duration (of a bond), FV (future value of an investment)

• Logical: Conditional statements, where a different formula can be used when a condition is fulfilled

• Text: Manipulate text strings and convert numbers to text (and vice versa)

19

Page 20: Lecture 2: Formulas & Functions

Formulas Tab

Categories:

• Date & Time: Create and manipulate date and time values. E.g. get today's date with TODAY() or get date and time with NOW()

• Lookup & Reference: Find data entries in the spreadsheet or link to online resources

• Maths & Trig: Find standard mathematical functions, such as logarithms, cosines and rounding

20

Page 21: Lecture 2: Formulas & Functions

More functions

Further categories (found under “More Functions”)

• Statistical: Stochastic functions:

• empirical investigations, e.g. computing sample means and sample standard deviations;

• probability theory, e.g. evaluating the normal distribution.

• Engineering: Some mathematical tools for engineers

• Cube: Database connection

• Information: Get information about data in another cell, e.g. is it a number?

• Compatibility: Functions from former Excel versions that were replaced and are only available to allow old files to be opened.Deprecated: Do not use, unless you have a valid reason to do so

• Web: Specialised functions to work with URLs and webservices.

21

Page 22: Lecture 2: Formulas & Functions

Standard Mathematical Functions

Most mathematical functions can be called as intuitively expected:

• LOG(num, base) computes the logarithm of num to the specified base (default base of 10 is used if omitted)

• LN(num) computes the natural logarithm of num (i.e. to the base of e)

• EXP(num) computes the exponential function (i.e. e to the power of num)

• SIN(num) computes the sine of an angle (given in radians)

• ABS(num) computes the absolute value of num.

• SIGN(num) returns the sign of a value (i.e. 1 for positive, -1 for negative values and 0 for 0)

22

Page 23: Lecture 2: Formulas & Functions

Sum functions

Excel has several functions for doing summation:

• SUM: Adds all the numbers in a range of cells'=SUM(A1:A6)' returns 21 (1+2+3+4+5+6)

• SUMSQ: Adds all the squares of the numbers ina range of cells.'=SUMSQ(A1:A6)' returns 91 (12 +…+62)

• SUMPRODUCT: Adds the product of corresponding entries of arrays:'=SUMPRODUCT(A1:A6, B1:B6)' returns 56 (1*6 + 2*5 + … + 6*1)

• SUMX2MY2: Sums the differences of the squares of corresponding entries of arrays:'=SUMX2MY2(A1:A6, B1:B6)' returns 0

(12−62)+(22−52)+…+(62−12)

23

Page 24: Lecture 2: Formulas & Functions

Auxiliary Cells

• On the previous slide we saw some examples of “complicated” summation formulas like SUMPRODUCT.

• A different way to do this is:

• Enter the formula =A1*B1 in cell C1.

• Copy that formula into cells C2:C6[At this point each cell in column C is the product of the two elements in columns A and B.]

• Now put =SUM(C1:C6) into cell C7.

• The value in C7 is exactly theSUMPRODUCT(A1:A6,B1:B6)

24

Example setup:

Page 25: Lecture 2: Formulas & Functions

Auxiliary Cells: Pros and Cons

Advantages

• Easier to remember

• Easier to understand

• Easy to check the formulas are doing what you expect.

Disadvantages

• Less “clean” – the spreadsheet is more cluttered. (Can be mitigated by hiding the column)

• May not “fit” neatly if you want to do lots of processing after the complicated sum.

Summary

As a guide use auxiliary cells if the spreadsheet’s purpose is to calculate things, and use the fancier formulas if the purpose is to communicate things.

25

Page 26: Lecture 2: Formulas & Functions

Nesting of functions

Mathematical functions are often nested: the output of a function is the input to another one

• Example: Computing the Euclidian norm (length) of a vector (x,y,z), so length is √(𝑥2+𝑦 2 +𝑧 2), is a composition of SUMSQ and SQRT:

• First SUMSQ computesthe sum of squares𝑥^2+𝑦^2+𝑧^2, which is directly passed on to SQRT to compute the square root.

• Alternatively you could use the formula '=SQRT(A2^2+B2^2+C2^2)' if you preferred.

26

Page 27: Lecture 2: Formulas & Functions

Dates

As far as Excel is concerned a date is a number with some specific formatting.

• Therefore you can do “number” operations on dates

• Some of these make sense:

• =date_1 - date_2 gives the number of days between the dates.

• =date + 5 gives the date 5 days after ‘date’

• But others do not:

• =date_1 + date_2 is nonsense.

• It’s up to you to make sure what you are doing makes sense!

27

Page 28: Lecture 2: Formulas & Functions

Text functions

Excel has many functions for manipulating text:

• '=CONCAT(text_1,text_2)' returns text_1 followed by text_2

• '=LEFT(text, num_chars)' returns the specified number of characters from the start of ‘text’ (see also RIGHT and MID)

• '=SEARCH(text_1,text_2)' returns the position of the text text_1 inside text_2:

• E.g, =SEARCH("W","Mark Walters") returns 6

• E.g, =SEARCH(" ","Mark Walters") returns 5

See also FIND

28

Page 29: Lecture 2: Formulas & Functions

Text functions (examples)

• We can use CONCAT to join firstnames with surnames:

• You need the " " in the middleor you would get MarkWalters

• To go from full name to first name is more complicated but we can use a nested formula:

• '=LEFT(full_name,SEARCH(" ",full_name)-1)'

29

Page 30: Lecture 2: Formulas & Functions

Flash Fill

• We just saw how we could use formulas to join or split first and surnames.

• Flash-fill can often do things like this automatically. It does this by noticing patterns, e.g. if you have a list of forenames in column A and surnames in column B, you can easily create a list of full names in column C:

• Type the first full name in column C1

• Select column C2 and click the Flash-fill button in the Data tab

• WARNING! Excel is guessing! Make sure it guessed right.

30

Page 31: Lecture 2: Formulas & Functions

Flash Fill versus Formulas

Advantages of FlashFill:

• Easy!

• Easy!

Advantages of Formulas:

• You know exactly what it is doing.

• It will update if the source data changes:

• E.g. if you change someone’s surname the fullname automatically updates.

• There are lots of different ways to do things in Excel.

31

Page 32: Lecture 2: Formulas & Functions

CONDITIONAL FORMULAS

Sometimes you want Excel to do different things based on some data:

• E.g. Suppose you have a list of students in column E and their module marks in Column F. You want Column G to say whether they passed or failed.

• You can use the 'IF' function:=IF(F2>=40, "PASS", "FAIL")

• The IF function checks whether thecondition 'F2>=40' is met: if it is, it returns the second argument;if the condition is not true it returns the third argument.

32

Page 33: Lecture 2: Formulas & Functions

CONDITIONAL FORMULAS

Many Excel functions can be combined with IF as a single function: SUMIF, COUNTIF, AVERAGEIF

• The simplest of these is COUNTIF. The syntax is'=COUNTIF(range,criteria)' which counts the number of cells which meet the condition.

• =COUNTIF(F2:F6,">=40") gives 4

33

Page 34: Lecture 2: Formulas & Functions

CONDITIONAL FORMULAS

SUMIF and AVERAGEIF are similar but have an extra argument: the syntax is

• '=AVERAGEIF(range, criteria, average_range)' which averages the elements in average_range where the corresponding element in range meets the condition.

• Almost always range andaverage_range are a singlecolumn or row and thecondition is asking about data in the correspondingcolumn of the same row

34

Page 35: Lecture 2: Formulas & Functions

Absolute referencing

Last week we saw that usually Excel will 'translate' a formula if you copy and paste it. When would you not want this?

35

Page 36: Lecture 2: Formulas & Functions

Absolute referencing

In the example below some students have coursework marks for three courseworks out of 30, 15 and 55respectively. I want the percentage for each coursework. I entered the formula =B2/C$1 into C2 and the copied it to all of C2:C9, E2,E9 and G2:G9. You can see the formulain E5 is "correct"

36

Page 37: Lecture 2: Formulas & Functions

Absolute referencing

In the example below I have listed some flights and their cost excluding VAT. I have entered the VAT rate in cell F1.

By using absolute referencing in the formula '=(1+$F$1)*B2' in C2 I was able to copy it down the whole of column C.

Having the VAT rate in cell F1 means it is easy to update the sheet if the rate changes.

37