Lecture 8: writing functions

Data: stock price

Prep work

library(tidyverse)
library(quantmod)
xts.stock <- getSymbols("AAPL", 
                        from = "2026-01-01", 
                        to = "2026-06-01",
                        auto.assign = F)

# list of companies (print out ALL.STOCK to see)
ALL.STOCK <- stockSymbols()

# try these functions to understand the returned R object
#class(xts.stock)
#plot(xts.stock$AAPL.Open)
#index(xts.stock)
#coredata(xts.stock)

# convert to data frame
df.stock <- data.frame(Date = index(xts.stock), coredata(xts.stock))

# change the name to get rid of "AAPL"
names(df.stock) <- sub(".*?\\.", "", names(df.stock))
head(df.stock)
        Date   Open   High    Low  Close   Volume Adjusted
1 2026-01-02 272.26 277.84 269.00 271.01 37838100 270.2744
2 2026-01-05 270.64 271.51 266.14 267.26 45647200 266.5345
3 2026-01-06 267.00 267.55 262.12 262.36 52352100 261.6478
4 2026-01-07 263.20 263.68 259.81 260.33 48309800 259.6233
5 2026-01-08 257.02 259.29 255.70 259.04 50419300 258.3369
6 2026-01-09 259.08 260.21 256.22 259.37 39997000 258.6659

Introduction

We have been using a lot of R functions. They all have the same format: function_name (argument1, argument2, …)

vec1 <- c(1:5)
mean(vec1)
[1] 3
  • Name: mean
  • Input: a numeric vector
  • Output: the average of all the numbers in the vector

Example 1

Write a function that calculates the difference of the first element in two given vectors.

  • Name: f.diff
  • Input: two vectors (a and b)
  • Output: a numeric value (result)
f.diff <- function(a,b){
  result <- a[1] - b[1]
  return(result)
}
  • you don’t need to declare the inputs (a and b)
  • you DO need to declare the output (result)

Try (recall, you can use tab to display arguments of the function):

f.diff(2, 3)
[1] -1
f.diff(c(1:5),c(5,10))
[1] -4
f.diff(a=c(1:5),b=c(5,10)) # full version
[1] -4

General structure of a function

  1. Function name: avoid using names that will override existing functions (e.g.: don’t write a function and name it something like plot or mean).
  2. Input(s): you can have more than one inputs
  3. Output: you can return only one R object; you can also return nothing
function_name <- function(input1, input2, ...){
  
  CODE TO CREATE "result"
    ASSUMING THAT "input1", "input2",... EXIST
  
  return(result)
}

Calling function

  • The objects a and b only live in the local environment (i.e., they only exist inside the function). Try print the value of a: R will say it is not found. When you call the function, you assign new values to a and b, which will then be passed into the function.

  • If you do not specify the argument names, the order of the arguments matter:

a=2; b=1
f.diff(a,b) 
[1] 1
f.diff(a,a) 
[1] 0
f.diff(b,a)
[1] -1
  • If you specify the argument names, the order of the arguments does NOT matter:
f.diff(b=5,a=3)
[1] -2
f.diff(b=b,a=a)
[1] 1
f.diff(b=a,a=b)
[1] -1

Example 2

Exercise: write a function that calculates the total length of 3 vectors. It should run like this:

vec1 <- runif(50) #randomly generate 50 numbers between 0 & 1
vec2 <- 20
vec3 <- c(1:4)

f.length(vec2,vec2,vec3)
[1] 6
f.length(vec1,vec2,vec3)
[1] 55
Solution
f.length <- function(a,b,c){
  result <- sum(length(a),length(b),length(c))
  return(result)
}

In addition, you can include things like plot(), print(), cat() as part of the function; you can also add a DEFAULT value to any input.

Show code
f.length <- function(a,b,c,print=T){
  result <- sum(length(a),length(b),length(c))
  
  if(print){
  cat("Lengths:", length(a),length(b),length(c))
  }
  return(result)
}

You can now use this function:

tot <- f.length(vec1,vec2,vec3); tot
Lengths: 50 1 4
[1] 55
tot <- f.length(vec1,vec2,vec3,print=F); tot
[1] 55
  • Total length (55 in this case) is stored in tot for later use
  • The argument print controls whether or not you want to print the information when running the function (default to TRUE)

Default values

For any R function, when an argument is not specified:

  • if there is a default, then the default is used
  • if there is no default, then an error message occurs
?cor
# you can see arguments with and without default values
cor(df.stock$Open, df.stock$Close)
cor(df.stock$Open) # not run
cor(y=df.stock$Open) # not run

Return multiple things in a list

Recall, you can only return one R object. If you want to return multiple things, you need to put them together in one list. For example, mymodel stores the list returned by the function lm (try mymodel$).

mymodel <- lm(df.stock$Close ~ df.stock$Open)

Example 3

  1. Write a function that will get the specified stock price information and returns a data frame, as well as name of the stock (recall, this information is stored in ALL.STOCK). It should work like this:
stock <- f.stock('GME',from="2020-12-01",to="2021-05-01")
stock$company
[1] "GameStop Corporation Common Stock"
stock$data
          Date    Open     High     Low   Close    Volume Adjusted
1   2020-12-01  4.2775   4.3500  3.9400  3.9500  50615600   3.9500
2   2020-12-02  3.9250   4.1700  3.8450  4.1450  31533600   4.1450
3   2020-12-03  4.1200   4.1600  3.9675  4.0300  25180000   4.0300
4   2020-12-04  4.0750   4.3225  4.0650  4.2250  35890800   4.2250
5   2020-12-07  4.2500   4.3750  4.0550  4.0875  29545200   4.0875
6   2020-12-08  4.0925   4.3025  3.9825  4.2350  64480800   4.2350
7   2020-12-09  3.4800   3.6825  3.3075  3.4150  97431600   3.4150
8   2020-12-10  3.2800   3.6025  3.2625  3.5300  30235600   3.5300
9   2020-12-11  3.4775   3.5000  3.2550  3.3275  29987600   3.3275
10  2020-12-14  3.3350   3.3575  3.0350  3.1800  40028400   3.1800
11  2020-12-15  3.1950   3.5050  3.1200  3.4625  32759200   3.4625
12  2020-12-16  3.4900   3.5775  3.3950  3.4625  23460400   3.4625
13  2020-12-17  3.4900   3.7450  3.3975  3.7075  32779600   3.7075
14  2020-12-18  3.9450   4.0750  3.7950  3.9075  66475200   3.9075
15  2020-12-21  3.9525   4.0875  3.8200  3.8825  39504400   3.8825
16  2020-12-22  4.0550   5.0100  4.0375  4.8650 122610800   4.8650
17  2020-12-23  5.0425   5.5875  4.7825  5.1425 103321200   5.1425
18  2020-12-24  5.2525   5.3700  4.9875  5.0375  25048800   5.0375
19  2020-12-28  5.3275   5.4925  5.0875  5.2475  35863600   5.2475
20  2020-12-29  5.2050   5.2675  4.6400  4.8450  36965600   4.8450
21  2020-12-30  4.8450   5.0000  4.7125  4.8150  23737600   4.8150
22  2020-12-31  4.8125   4.9500  4.7000  4.7100  27690800   4.7100
23  2021-01-04  4.7500   4.7750  4.2875  4.3125  40090000   4.3125
24  2021-01-05  4.3375   4.5200  4.3075  4.3425  19846000   4.3425
25  2021-01-06  4.3350   4.7450  4.3325  4.5900  24224800   4.5900
26  2021-01-07  4.6175   4.8625  4.5050  4.5200  24517200   4.5200
27  2021-01-08  4.5450   4.5750  4.2700  4.4225  25928000   4.4225
28  2021-01-11  4.8525   5.1625  4.7525  4.9850  59632000   4.9850
29  2021-01-12  4.9900   5.1000  4.8300  4.9875  28242800   4.9875
30  2021-01-13  5.1050   9.6625  5.0075  7.8500 578006800   7.8500
31  2021-01-14  9.5225  10.7650  8.2625  9.9775 374869600   9.9775
32  2021-01-15  9.6225  10.1875  8.5025  8.8750 187465600   8.8750
33  2021-01-19 10.3875  11.3800  9.1600  9.8400 298887600   9.8400
34  2021-01-20  9.3425  10.2975  9.0150  9.7800 133887200   9.7800
35  2021-01-21  9.8075  11.1875  9.2500 10.7575 224867600  10.7575
36  2021-01-22 10.6475  19.1900 10.5800 16.2525 788631600  16.2525
37  2021-01-25 24.1825  39.7950 15.2825 19.1975 711496000  19.1975
38  2021-01-26 22.1400  37.5000 20.0500 36.9950 714352000  36.9950
39  2021-01-27 88.7075  95.0000 62.2500 86.8775 373586800  86.8775
40  2021-01-28 66.2500 120.7500 28.0625 48.4000 235263200  48.4000
41  2021-01-29 94.9275 103.4950 62.5000 81.2500 202264400  81.2500
42  2021-02-01 79.1400  80.5000 53.0000 56.2500 149528800  56.2500
43  2021-02-02 35.1900  39.5000 18.5550 22.5000 312732400  22.5000
44  2021-02-03 28.0025  28.3500 21.3125 23.1025 170794000  23.1025
45  2021-02-04 22.7975  22.8750 13.3325 13.3750 249709200  13.3750
46  2021-02-05 13.5100  23.7500 12.7725 15.9425 325380000  15.9425
47  2021-02-08 18.1025  18.1650 14.5050 15.0000 102749200  15.0000
48  2021-02-09 14.1525  14.2500 11.6300 12.5775 107372400  12.5775
49  2021-02-10 12.6925  15.7075 11.6375 12.8000 145820000  12.8000
50  2021-02-11 12.5025  13.8300 12.0550 12.7750  52226800  12.7750
51  2021-02-12 12.6875  13.8100 12.0125 13.1000  58293200  13.1000
52  2021-02-16 13.1650  13.3750 12.2600 12.3775  32700000  12.3775
53  2021-02-17 12.4425  12.7975 11.1400 11.4850  36747200  11.4850
54  2021-02-18 12.1225  12.2175 10.1625 10.1725  95962400  10.1725
55  2021-02-19 10.3200  10.9725  9.6250 10.1475  59312800  10.1475
56  2021-02-22 11.6725  12.1275 10.6000 11.5000  77904000  11.5000
57  2021-02-23 11.2425  11.5575 10.0000 11.2425  30260800  11.2425
58  2021-02-24 11.1750  22.9275 11.1750 22.9275 332446800  22.9275
59  2021-02-25 42.3900  46.1700 25.2500 27.1825 601235200  27.1825
60  2021-02-26 29.3650  35.7250 21.5000 25.4350 368776800  25.4350
61  2021-03-01 26.1350  33.4975 24.9925 30.1000 199176000  30.1000
62  2021-03-02 29.2325  33.3000 28.0500 29.5450 135132000  29.5450
63  2021-03-03 30.6275  31.9375 28.2800 31.0450  77095600  31.0450
64  2021-03-04 31.2500  36.9675 28.8250 33.0875 130427600  33.0875
65  2021-03-05 32.0425  37.8825 31.8750 34.4350 122934800  34.4350
66  2021-03-08 38.7225  52.7175 36.5250 48.6250 254262400  48.6250
67  2021-03-09 54.4275  62.4625 52.1275 61.7250 156397200  61.7250
68  2021-03-10 67.3575  87.1250 43.0000 66.2500 286282400  66.2500
69  2021-03-11 60.4100  70.3750 58.1500 65.0000 113250000  65.0000
70  2021-03-12 68.7500  73.8750 65.5675 66.1250 103383600  66.1250
71  2021-03-15 69.3800  70.7500 51.5000 55.0350  96401600  55.0350
72  2021-03-16 50.7900  55.1750 43.0875 52.0425 141691600  52.0425
73  2021-03-17 54.4600  57.8675 51.0000 52.4525  65926400  52.4525
74  2021-03-18 53.5000  54.7200 48.9125 50.4375  47059600  50.4375
75  2021-03-19 48.9325  56.7500 45.6650 50.0675  98709200  50.0675
76  2021-03-22 51.3150  52.5900 46.5500 48.6225  40246000  48.6225
77  2021-03-23 49.3750  50.4375 44.3875 45.4375  57716400  45.4375
78  2021-03-24 39.4950  41.7425 29.6550 30.0850  96711600  30.0850
79  2021-03-25 30.8725  46.8750 29.2250 45.9375 203849200  45.9375
80  2021-03-26 49.4200  54.7325 40.8150 45.2500 149722800  45.2500
81  2021-03-29 45.1875  48.4800 43.3775 45.3250  40168800  45.3250
82  2021-03-30 46.8750  51.0750 45.5000 48.6150  68379600  48.6150
83  2021-03-31 49.3750  49.8650 46.7775 47.4550  33575200  47.4550
84  2021-04-01 48.3400  49.2425 45.9000 47.8625  37337200  47.8625
85  2021-04-05 42.7500  48.7500 41.2025 46.7375  56282000  46.7375
86  2021-04-06 46.3025  48.0000 45.8900 46.1250  24873200  46.1250
87  2021-04-07 45.8050  46.1250 44.0275 44.4925  19073200  44.4925
88  2021-04-08 46.4700  46.4700 41.0750 42.5650  40189600  42.5650
89  2021-04-09 42.4250  42.8950 38.2500 39.5900  37849600  39.5900
90  2021-04-12 39.5275  40.9750 33.7525 35.2725  66734400  35.2725
91  2021-04-13 35.4700  36.3450 33.0000 35.2475  27227600  35.2475
92  2021-04-14 35.8925  43.5225 35.7500 41.6325  84552400  41.6325
93  2021-04-15 40.7500  41.5625 38.2000 39.1100  31427200  39.1100
94  2021-04-16 39.0000  40.0500 37.8125 38.6725  20858800  38.6725
95  2021-04-19 42.9500  43.8000 40.5825 41.0925  42080800  41.0925
96  2021-04-20 41.0350  41.2225 38.3400 39.6325  18634400  39.6325
97  2021-04-21 39.6000  40.7450 38.9800 39.6275  15251200  39.6275
98  2021-04-22 39.7750  39.7750 36.7150 37.7925  17383200  37.7925
99  2021-04-23 37.2500  38.4150 36.1750 37.7950  16116400  37.7950
100 2021-04-26 37.7450  43.6700 37.1900 42.2325  42128400  42.2325
101 2021-04-27 45.8875  47.0000 43.2925 44.4425  35730800  44.4425
102 2021-04-28 43.0250  45.4950 41.5075 44.6450  23155600  44.6450
103 2021-04-29 44.1400  45.2150 42.6875 44.0475  15041200  44.0475
104 2021-04-30 43.7500  45.9500 43.1750 43.3975  17734400  43.3975
# "from" and "to" has default values, so they don't have to be specified
stock <- f.stock('AAPL')
stock$company
[1] "Apple Inc. - Common Stock"
stock$data
          Date   Open   High    Low  Close    Volume Adjusted
1   2026-01-02 272.26 277.84 269.00 271.01  37838100 270.2743
2   2026-01-05 270.64 271.51 266.14 267.26  45647200 266.5345
3   2026-01-06 267.00 267.55 262.12 262.36  52352100 261.6478
4   2026-01-07 263.20 263.68 259.81 260.33  48309800 259.6233
5   2026-01-08 257.02 259.29 255.70 259.04  50419300 258.3368
6   2026-01-09 259.08 260.21 256.22 259.37  39997000 258.6659
7   2026-01-12 259.16 261.30 256.80 260.25  45263800 259.5435
8   2026-01-13 258.72 261.81 258.39 261.05  45730800 260.3413
9   2026-01-14 259.49 261.82 256.71 259.96  40019400 259.2543
10  2026-01-15 260.65 261.04 257.05 258.21  39388600 257.5091
11  2026-01-16 257.90 258.90 254.93 255.53  72142800 254.8363
12  2026-01-20 252.73 254.79 243.42 246.70  80267500 246.0303
13  2026-01-21 248.70 251.56 245.18 247.65  54641700 246.9778
14  2026-01-22 249.20 251.00 248.15 248.35  39708300 247.6758
15  2026-01-23 247.32 249.41 244.68 248.04  41689000 247.3667
16  2026-01-26 251.48 256.56 249.80 255.41  55969200 254.7167
17  2026-01-27 259.17 261.95 258.21 258.27  49648300 257.5689
18  2026-01-28 257.65 258.86 254.51 256.44  41288000 255.7439
19  2026-01-29 258.00 259.65 254.41 258.28  67253000 257.5789
20  2026-01-30 255.17 261.90 252.18 259.48  92443400 258.7756
21  2026-02-02 260.03 270.49 259.21 270.01  73913400 269.2771
22  2026-02-03 269.20 271.88 267.61 269.48  64394700 268.7485
23  2026-02-04 272.29 278.95 272.29 276.49  90545700 275.7394
24  2026-02-05 278.13 279.50 273.23 275.91  52977400 275.1610
25  2026-02-06 277.12 280.91 276.93 278.12  50453400 277.3650
26  2026-02-09 277.91 278.20 271.70 274.62  44623400 274.1308
27  2026-02-10 274.89 275.37 272.94 273.68  34376900 273.1924
28  2026-02-11 274.70 280.18 274.45 275.50  51931300 275.0092
29  2026-02-12 275.59 275.72 260.18 261.73  81077200 261.2638
30  2026-02-13 262.01 262.23 255.45 255.78  56290700 255.3244
31  2026-02-17 258.05 266.29 255.54 263.88  58469100 263.4099
32  2026-02-18 263.60 266.82 262.45 264.35  34203300 263.8791
33  2026-02-19 262.60 264.48 260.05 260.58  30845300 260.1158
34  2026-02-20 258.97 264.75 258.16 264.58  42070500 264.1087
35  2026-02-23 263.49 269.43 263.38 266.18  37308200 265.7058
36  2026-02-24 267.86 274.89 267.71 272.14  47014600 271.6552
37  2026-02-25 271.78 274.94 271.05 274.23  33714300 273.7415
38  2026-02-26 274.95 276.11 270.80 272.95  32345100 272.4638
39  2026-02-27 272.81 272.81 262.89 264.18  72366500 263.7094
40  2026-03-02 262.41 266.53 260.20 264.72  41827900 264.2484
41  2026-03-03 263.48 265.56 260.13 263.75  38568900 263.2802
42  2026-03-04 264.65 266.15 261.42 262.52  39803100 262.0523
43  2026-03-05 260.79 261.56 257.25 260.29  49658600 259.8263
44  2026-03-06 258.63 258.77 254.37 257.46  41120000 257.0013
45  2026-03-09 255.69 261.15 253.68 259.88  38218500 259.4171
46  2026-03-10 257.65 262.48 256.95 260.83  30590800 260.3654
47  2026-03-11 261.09 262.13 259.55 260.81  26218900 260.3454
48  2026-03-12 258.66 258.95 254.18 255.76  40794000 255.3044
49  2026-03-13 255.48 256.33 249.52 250.12  36930000 249.6744
50  2026-03-16 252.11 253.89 249.88 252.82  32074200 252.3696
51  2026-03-17 252.96 255.13 252.18 254.23  32361600 253.7771
52  2026-03-18 252.63 254.94 249.00 249.94  35757900 249.4948
53  2026-03-19 249.40 251.83 247.30 248.96  34864100 248.5165
54  2026-03-20 247.98 249.20 246.00 247.99  88331100 247.5482
55  2026-03-23 253.97 254.60 250.28 251.49  40546100 251.0420
56  2026-03-24 250.35 254.83 249.55 251.64  45152300 251.1917
57  2026-03-25 254.10 255.00 251.60 252.62  28476700 252.1700
58  2026-03-26 252.12 257.00 250.77 252.89  41796700 252.4395
59  2026-03-27 253.90 255.49 248.07 248.80  47900000 248.3568
60  2026-03-30 250.07 250.87 245.51 246.63  39446200 246.1907
61  2026-03-31 247.91 255.48 247.10 253.79  49598100 253.3379
62  2026-04-01 254.08 256.18 253.33 255.63  40059400 255.1746
63  2026-04-02 254.20 256.13 250.65 255.92  31289400 255.4641
64  2026-04-06 256.51 262.16 256.46 258.86  29329900 258.3989
65  2026-04-07 256.16 256.20 245.70 253.50  62148000 253.0484
66  2026-04-08 258.45 259.75 256.53 258.90  41032800 258.4388
67  2026-04-09 259.00 261.12 256.07 260.49  28121600 260.0260
68  2026-04-10 259.98 262.19 259.02 260.48  31291500 260.0160
69  2026-04-13 259.73 260.18 256.66 259.20  36234700 258.7383
70  2026-04-14 259.25 261.93 257.19 258.83  48370700 258.3689
71  2026-04-15 258.16 266.56 257.81 266.43  49913500 265.9554
72  2026-04-16 266.80 267.16 261.27 263.40  43323100 262.9308
73  2026-04-17 266.96 272.30 266.72 270.23  61436200 269.7486
74  2026-04-20 270.33 274.28 270.29 273.05  36590200 272.5636
75  2026-04-21 271.50 272.80 265.40 266.17  50209800 265.6959
76  2026-04-22 267.82 273.74 266.87 273.17  43249200 272.6834
77  2026-04-23 275.05 275.77 271.65 273.43  33399600 272.9429
78  2026-04-24 272.76 273.06 269.65 271.06  38157100 270.5771
79  2026-04-27 266.09 268.36 265.07 267.61  41466800 267.1333
80  2026-04-28 272.34 273.23 268.66 270.71  40018900 270.2278
81  2026-04-29 267.55 271.04 267.04 270.17  30047900 269.6887
82  2026-04-30 270.50 276.00 268.14 271.35  91848200 270.8666
83  2026-05-01 278.86 287.22 278.37 280.14  79915400 279.6410
84  2026-05-04 279.66 280.63 274.86 276.83  46668400 276.3368
85  2026-05-05 276.93 284.57 276.50 284.18  49311700 283.6738
86  2026-05-06 281.92 288.03 281.07 287.51  58336100 286.9979
87  2026-05-07 289.27 292.13 285.78 287.44  45224300 286.9280
88  2026-05-08 290.01 294.76 290.00 293.32  52692800 292.7975
89  2026-05-11 291.98 293.88 290.23 292.68  42247300 292.4278
90  2026-05-12 292.56 295.27 292.56 294.80  45748100 294.5460
91  2026-05-13 293.50 300.92 293.50 298.87  52684300 298.6125
92  2026-05-14 299.82 300.45 295.38 298.21  35324900 297.9530
93  2026-05-15 297.90 303.20 296.52 300.23  54862800 299.9713
94  2026-05-18 300.24 300.66 294.91 297.84  34483000 297.5833
95  2026-05-19 296.97 300.51 296.35 298.97  42243600 298.7124
96  2026-05-20 298.18 302.80 298.08 302.25  38229800 301.9896
97  2026-05-21 301.06 305.54 300.40 304.99  42965100 304.7272
98  2026-05-22 306.12 311.40 305.84 308.82  43670200 308.5539
99  2026-05-26 309.56 311.82 307.67 308.33  48000500 308.0643
100 2026-05-27 308.33 313.26 308.30 310.85  50430900 310.5822
101 2026-05-28 310.68 312.80 309.57 312.51  48220400 312.2407
102 2026-05-29 311.78 315.00 309.53 312.06  70026800 311.7911
103 2026-06-01 309.63 310.94 305.02 306.31  48849900 306.0461
104 2026-06-02 307.46 315.45 306.69 315.20  44534700 314.9284
105 2026-06-03 314.18 316.94 308.85 310.26  50836700 309.9926
106 2026-06-04 313.23 313.54 309.65 311.23  44869100 310.9618
107 2026-06-05 312.86 315.17 307.15 307.34  65310500 307.0752
108 2026-06-08 308.74 317.40 301.17 301.54  77949100 301.2802
109 2026-06-09 300.28 300.75 287.78 290.55  70108800 290.2996
110 2026-06-10 290.74 294.75 287.38 291.58  52793300 291.3287
111 2026-06-11 293.72 297.00 289.59 295.63  42572500 295.3752
112 2026-06-12 296.03 297.14 289.62 291.13  38742100 290.8792
113 2026-06-15 294.12 297.78 291.70 296.42  45732600 296.1646
114 2026-06-16 295.25 300.48 293.97 299.24  39874400 298.9821
115 2026-06-17 300.85 302.07 294.36 295.95  42745100 295.6950
116 2026-06-18 298.11 300.57 295.62 298.01  85962200 297.7532
117 2026-06-22 297.31 302.42 296.76 297.01  44879900 296.7541
118 2026-06-23 297.54 301.64 294.18 294.30  52010900 294.0464
119 2026-06-24 295.36 299.70 292.94 293.08  53081900 292.8274
120 2026-06-25 287.40 288.80 273.75 275.15 107013700 274.9129
121 2026-06-26 275.00 285.95 274.21 283.78 261775500 283.5355
122 2026-06-29 286.73 288.37 279.85 281.74  66427000 281.4972
123 2026-06-30 281.17 289.94 280.70 289.36  65100200 289.1107
124 2026-07-01 293.44 296.59 289.20 294.38  50164200 294.1263
125 2026-07-02 294.12 309.42 293.68 308.63  75352800 308.3640
126 2026-07-06 307.36 314.20 307.00 312.66  53590000 312.3906
127 2026-07-07 315.29 315.48 310.15 310.66  42490000 310.3923
128 2026-07-08 311.91 314.82 307.05 313.39  41323500 313.1200
129 2026-07-09 310.51 316.53 308.16 316.22  48124500 315.9475
130 2026-07-10 314.72 316.91 312.17 315.32  34132300 315.0483
131 2026-07-13 317.02 323.45 315.78 317.31  43257800 317.0366
132 2026-07-14 313.76 316.19 311.91 314.86  36336800 314.5887
133 2026-07-15 317.62 328.73 317.32 327.50  60957600 327.2178
134 2026-07-16 328.01 334.68 326.79 333.26  62970600 332.9728
135 2026-07-17 331.98 334.99 329.00 333.74  63407100 333.4524
136 2026-07-20 333.51 333.71 323.68 326.59  53468000 326.3086
137 2026-07-21 323.13 329.60 322.22 327.74  41338900 327.4576
138 2026-07-22 327.87 329.00 323.34 325.89  38755900 325.6092
139 2026-07-23 321.73 323.30 319.35 321.66  40840800 321.3828
140 2026-07-24 321.79 334.37 321.62 333.02  47489400 332.7330
141 2026-07-27 334.54 339.57 334.02 336.91  49604300 336.6197
142 2026-07-28 340.03 342.89 335.60 340.08  51859000 339.7869
143 2026-07-29 339.73 344.57 337.35 338.19  56090800 337.8986
144 2026-07-30 333.10 334.75 329.59 333.43  74817800 333.1427
145 2026-07-31 304.81 310.69 300.00 308.91 132489100 308.6438
146 2026-08-03 309.58 311.80 302.56 303.42  75052000 303.1586
147 2026-08-04 302.73 310.42 301.32 309.38  68001000 309.1134
148 2026-08-05 309.36 311.71 305.67 311.00  49438800 310.7320
149 2026-08-06 314.34 316.29 309.23 312.41  46139900 312.1408
150 2026-08-07 311.45 314.81 310.74 313.33  34437200 313.0600
151 2026-08-10 306.83 308.26 304.61 308.26  44812500 308.2600
152 2026-08-11 307.75 309.97 302.79 304.91  37476700 304.9100
153 2026-08-12 305.10 305.66 300.57 302.25  41657800 302.2500
154 2026-08-13 304.21 306.00 302.05 305.26  40349300 305.2600
155 2026-08-14 306.00 307.49 304.30 305.93  28229400 305.9300
156 2026-08-17 306.21 307.66 302.94 305.59  38169300 305.5900
157 2026-08-18 307.58 311.49 305.74 310.03  53424500 310.0300
158 2026-08-19 310.14 319.28 309.60 316.83  50505600 316.8300
159 2026-08-20 317.46 320.28 310.65 311.30  40959200 311.3000
160 2026-08-21 312.05 312.38 307.01 309.35  46876800 309.3500
161 2026-08-24 311.47 313.36 309.97 310.34  34673600 310.3400
162 2026-08-25 310.79 313.59 308.21 309.90  25869800 309.9000
163 2026-08-26 310.30 315.43 308.80 313.45  34024500 313.4500
164 2026-08-27 310.55 315.40 309.40 314.58  32419200 314.5800
165 2026-08-28 316.85 322.37 315.45 319.70  38649400 319.7000
166 2026-08-31 319.60 321.24 312.80 316.85  41242700 316.8500
167 2026-09-01 316.98 327.30 314.73 325.13  53167400 325.1300
168 2026-09-02 326.87 328.40 323.53 324.96  33776400 324.9600
169 2026-09-03 324.87 330.81 324.11 328.21  37225800 328.2100
170 2026-09-04 328.31 328.93 317.86 319.97  39606900 319.9700
171 2026-09-08 317.10 320.70 314.90 316.22  35477100 316.2200
172 2026-09-09 315.49 319.15 309.90 315.34  65640000 315.3400
173 2026-09-10 316.67 326.74 316.51 326.57  70011900 326.5700
174 2026-09-11 327.45 336.22 326.30 332.27  50716900 332.2700
175 2026-09-14 334.79 335.50 331.34 333.08  39269100 333.0800
176 2026-09-15 330.14 331.78 328.35 331.34  31748200 331.3400
177 2026-09-16 332.53 335.48 330.70 332.41  35981000 332.4100
178 2026-09-17 334.77 338.34 330.18 337.00  36700200 337.0000
179 2026-09-18 337.91 338.49 332.53 336.13  86588200 336.1300
180 2026-09-21 335.28 339.64 333.05 338.98  34999200 338.9800
181 2026-09-22 340.14 345.34 338.75 339.75  40711800 339.7500
182 2026-09-23 341.08 341.80 335.50 337.02  31658800 337.0200
183 2026-09-24 336.72 338.91 334.30 335.92  24733100 335.9200
184 2026-09-25 336.04 341.67 334.53 341.07  30002500 341.0700
185 2026-09-28 340.37 342.99 338.04 338.40  32820800 338.4000
186 2026-09-29 336.97 337.09 328.70 329.40  38478000 329.4000
187 2026-09-30 330.80 339.50 330.14 333.02  49988600 333.0200
188 2026-10-01 330.00 332.48 325.81 330.32  36306300 330.3200
189 2026-10-02 333.26 334.54 330.61 333.69  33278600 333.6900
190 2026-10-05 332.82 336.21 331.65 332.89  34400900 332.8900
191 2026-10-06 332.28 334.38 330.62 333.63  30449000 333.6300
192 2026-10-07 336.96 338.67 332.78 336.67  34147900 336.6700
193 2026-10-08 336.82 341.57 335.90 340.42  35332400 340.4200
194 2026-10-09 331.70 338.61 330.70 336.64  37881200 336.6400
195 2026-10-12 338.22 342.63 337.60 340.97  25900600 340.9700
196 2026-10-13 344.67 348.15 340.74 341.27  38983800 341.2700
197 2026-10-14 344.61 347.09 341.72 342.96  50061100 342.9600
198 2026-10-15 346.95 347.85 341.60 342.00  39767300 342.0000
199 2026-10-16 346.09 347.82 340.46 343.46  59946400 343.4600
200 2026-10-19 340.78 346.00 340.12 342.94  34573200 342.9400
201 2026-10-20 339.86 343.06 338.70 339.50  35851400 339.5000
202 2026-10-21 339.36 345.25 338.83 343.99  35614100 343.9900
203 2026-10-22 344.94 348.36 342.28 348.22  46898900 348.2200
204 2026-10-23 349.12 351.73 345.75 347.73  39539700 347.7300
205 2026-10-26 343.92 347.09 341.03 343.91  42743600 343.9100
206 2026-10-27 343.17 347.78 342.39 347.10  39984400 347.1000
207 2026-10-28 346.92 347.18 343.52 343.83  35714300 343.8300
208 2026-10-29 348.00 351.43 347.30 348.91  42582100 348.9100
209 2026-10-30 346.72 349.19 334.35 344.63  67841600 344.6300
210 2026-11-02 342.67 343.80 335.82 338.32  64757700 338.3200
211 2026-11-03 325.58 334.50 325.33 331.90  63160000 331.9000
212 2026-11-04 341.57 354.50 340.99 349.51  89637700 349.5100
213 2026-11-05 353.89 355.38 342.06 344.17  75678900 344.1700
214 2026-11-06 338.73 345.79 338.30 342.37  44703800 342.3700
215 2026-11-09 345.00 352.33 344.78 350.69  48753100 350.6900
216 2026-11-10 351.35 353.87 346.46 348.51  29756100 348.5100
217 2026-11-11 349.55 355.91 349.33 355.63  34284700 355.6300
Solution
f.stock <- function(name, from="2026-01-01", to=Sys.Date()){
  #name='NVS'
  xts.stock <- getSymbols(name, from = from, to = to, auto.assign = F) 
  df.stock <- data.frame(Date = index(xts.stock), coredata(xts.stock))
  names(df.stock) <- sub(".*?\\.", "", names(df.stock))
  
  company <- ALL.STOCK %>%
    filter(Symbol == name) %>%
    pull(Name)
  
  result <- list(data=df.stock,company=company)
  return(result)
}

Note 1: you can set some input values (in my function, I wrote name=‘NVS’); comment those out right away in case you forget to do so later!

Note 2: what does Sys.Date() return? Run it to find out.

  1. Add an IF condition in the f.stock function to check if the input of company name is valid.
stock <- f.stock('abc')
[1] "Invalid Name Input"
stock <- f.stock(1)
[1] "Invalid Name Input"
stock <- f.stock('NVS')
Solution
f.stock <- function(name, from="2026-01-01", to=Sys.Date()){
  #name='a'
  company = ALL.STOCK %>%
  filter(Symbol == name) %>%
  pull(Name)
  
  if(length(company)==0){
    print('Invalid Name Input')
  }else{
     xts.stock <- getSymbols(name, from = from, to = to, auto.assign = F) 
  df.stock <- data.frame(Date = index(xts.stock), coredata(xts.stock))
  names(df.stock) <- sub(".*?\\.", "", names(df.stock))

  result <- list(data=df.stock,company=company)
    return(result)
  }
}

Example 4: return nothing

Write a function that will generate the following ggplot:

stock <- f.stock('GME',from="2020-12-01",to="2021-05-01")
f.plot(df=stock$data,title=stock$company)
Solution
f.plot <- function(df, title){
    df %>% 
    pivot_longer(names_to ='Type',values_to = 'Price', c(Open, Close)) %>%
    ggplot(aes(x=Date, y=Price, color=Type)) +
    geom_line() +
    ggtitle(title) +
    theme_minimal()
}