library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## โ”€โ”€ Attaching core tidyquant packages โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ tidyquant 1.0.11 โ”€โ”€
## โœ” PerformanceAnalytics 2.0.8      โœ” TTR                  0.24.4
## โœ” quantmod             0.4.28     โœ” xts                  0.14.1
## โ”€โ”€ Conflicts โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ tidyquant_conflicts() โ”€โ”€
## โœ– zoo::as.Date()                 masks base::as.Date()
## โœ– zoo::as.Date.numeric()         masks base::as.Date.numeric()
## โœ– PerformanceAnalytics::legend() masks graphics::legend()
## โœ– quantmod::summary()            masks base::summary()
## โ„น Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyverse)
## โ”€โ”€ Attaching core tidyverse packages โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ tidyverse 2.0.0 โ”€โ”€
## โœ” dplyr     1.1.4     โœ” readr     2.1.5
## โœ” forcats   1.0.1     โœ” stringr   1.5.2
## โœ” ggplot2   4.0.0     โœ” tibble    3.3.0
## โœ” lubridate 1.9.4     โœ” tidyr     1.3.1
## โœ” purrr     1.1.0     
## โ”€โ”€ Conflicts โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ tidyverse_conflicts() โ”€โ”€
## โœ– dplyr::filter() masks stats::filter()
## โœ– dplyr::first()  masks xts::first()
## โœ– dplyr::lag()    masks stats::lag()
## โœ– dplyr::last()   masks xts::last()
## โ„น Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(timetk)
## 
## Attaching package: 'timetk'
## The following object is masked from 'package:tidyquant':
## 
##     FANG
read_delim("/Users/Frezza/Downloads/tej_day_price_2024_20250630.txt", delim = "\t")
## Rows: 337347 Columns: 12
## โ”€โ”€ Column specification โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
## Delimiter: "\t"
## chr (3): CO_ID, TSE Sector, English Short Name
## dbl (9): Date, TSE ID, Open(NTD), High(NTD), Low(NTD), Close(NTD), Volume(10...
## 
## โ„น Use `spec()` to retrieve the full column specification for this data.
## โ„น Specify the column types or set `show_col_types = FALSE` to quiet this message.
## # A tibble: 337,347 ร— 12
##    CO_ID             Date `TSE ID` `TSE Sector` `English Short Name` `Open(NTD)`
##    <chr>            <dbl>    <dbl> <chr>        <chr>                      <dbl>
##  1 1101 TCC        2.02e7     1101 01           TCC                         32.5
##  2 1102 ACC        2.02e7     1102 01           ACC                         37.3
##  3 1103 CHC        2.02e7     1103 01           CHC                         17.8
##  4 1104 UCC        2.02e7     1104 01           UCC                         26.1
##  5 1108 Lucky Cemโ€ฆ 2.02e7     1108 01           Lucky Cement                14.2
##  6 1109 HSINGTA    2.02e7     1109 01           HSINGTA                     16.2
##  7 1110 Tuna Cemeโ€ฆ 2.02e7     1110 01           Tuna Cement                 18.3
##  8 1201 WEI CHUAN  2.02e7     1201 02           WEI CHUAN                   18.2
##  9 1203 Ve Wong    2.02e7     1203 02           Ve Wong                     55.8
## 10 1210 GREATWALL  2.02e7     1210 02           GREATWALL                   53.8
## # โ„น 337,337 more rows
## # โ„น 6 more variables: `High(NTD)` <dbl>, `Low(NTD)` <dbl>, `Close(NTD)` <dbl>,
## #   `Volume(1000S)` <dbl>, `Amount(NTD1000)` <dbl>, `Market Cap.(NTD MN)` <dbl>
data_tsv <- read_tsv("/Users/Frezza/Downloads/tej_day_price_2024_20250630.txt")
## Rows: 337347 Columns: 12
## โ”€โ”€ Column specification โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
## Delimiter: "\t"
## chr (3): CO_ID, TSE Sector, English Short Name
## dbl (9): Date, TSE ID, Open(NTD), High(NTD), Low(NTD), Close(NTD), Volume(10...
## 
## โ„น Use `spec()` to retrieve the full column specification for this data.
## โ„น Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(data_tsv)
## spc_tbl_ [337,347 ร— 12] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ CO_ID              : chr [1:337347] "1101 TCC" "1102 ACC" "1103 CHC" "1104 UCC" ...
##  $ Date               : num [1:337347] 20240102 20240102 20240102 20240102 20240102 ...
##  $ TSE ID             : num [1:337347] 1101 1102 1103 1104 1108 ...
##  $ TSE Sector         : chr [1:337347] "01" "01" "01" "01" ...
##  $ English Short Name : chr [1:337347] "TCC" "ACC" "CHC" "UCC" ...
##  $ Open(NTD)          : num [1:337347] 32.5 37.3 17.8 26.1 14.2 ...
##  $ High(NTD)          : num [1:337347] 32.5 37.4 17.8 26.2 14.2 ...
##  $ Low(NTD)           : num [1:337347] 32.3 36.9 17.6 26 14 ...
##  $ Close(NTD)         : num [1:337347] 32.4 37 17.6 26.1 14.1 ...
##  $ Volume(1000S)      : num [1:337347] 14937 6223 171 260 442 ...
##  $ Amount(NTD1000)    : num [1:337347] 518751 256522 3240 7736 6992 ...
##  $ Market Cap.(NTD MN): num [1:337347] 262026 145941 14896 19995 6395 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   CO_ID = col_character(),
##   ..   Date = col_double(),
##   ..   `TSE ID` = col_double(),
##   ..   `TSE Sector` = col_character(),
##   ..   `English Short Name` = col_character(),
##   ..   `Open(NTD)` = col_double(),
##   ..   `High(NTD)` = col_double(),
##   ..   `Low(NTD)` = col_double(),
##   ..   `Close(NTD)` = col_double(),
##   ..   `Volume(1000S)` = col_double(),
##   ..   `Amount(NTD1000)` = col_double(),
##   ..   `Market Cap.(NTD MN)` = col_double()
##   .. )
##  - attr(*, "problems")=<externalptr>
# fetch data by using read.delim
data_delim <- read.delim("/Users/Frezza/Downloads/tej_day_price_2024_20250630.txt", sep = "\t")
glimpse(data_delim)
## Rows: 337,347
## Columns: 12
## $ CO_ID               <chr> "1101 TCC", "1102 ACC", "1103 CHC", "1104 UCC", "1โ€ฆ
## $ Date                <int> 20240102, 20240102, 20240102, 20240102, 20240102, โ€ฆ
## $ TSE.ID              <int> 1101, 1102, 1103, 1104, 1108, 1109, 1110, 1201, 12โ€ฆ
## $ TSE.Sector          <int> 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,โ€ฆ
## $ English.Short.Name  <chr> "TCC", "ACC", "CHC", "UCC", "Lucky Cement", "HSINGโ€ฆ
## $ Open.NTD.           <dbl> 32.5373, 37.2642, 17.7825, 26.0628, 14.1679, 16.18โ€ฆ
## $ High.NTD.           <dbl> 32.5373, 37.4442, 17.7825, 26.1505, 14.1679, 16.26โ€ฆ
## $ Low.NTD.            <dbl> 32.3038, 36.9492, 17.5953, 25.9750, 14.0343, 16.13โ€ฆ
## $ Close.NTD.          <dbl> 32.3972, 37.0392, 17.6421, 26.0628, 14.0788, 16.18โ€ฆ
## $ Volume.1000S.       <int> 14937, 6223, 171, 260, 442, 228, 57, 126, 48, 1849โ€ฆ
## $ Amount.NTD1000.     <int> 518751, 256522, 3240, 7736, 6992, 4159, 1075, 2409โ€ฆ
## $ Market.Cap..NTD.MN. <int> 262026, 145941, 14896, 19995, 6395, 6209, 10754, 9โ€ฆ
data_renamed <- data_delim %>%
  # Rename specific columns by their presumed old name (X#) or actual position
  rename(
    date = `Date`,
    id = `TSE.ID`,
    name = `English.Short.Name`, 
    price = `Close.NTD.`,
    cap = `Market.Cap..NTD.MN.`
  )

glimpse(data_renamed)
## Rows: 337,347
## Columns: 12
## $ CO_ID           <chr> "1101 TCC", "1102 ACC", "1103 CHC", "1104 UCC", "1108 โ€ฆ
## $ date            <int> 20240102, 20240102, 20240102, 20240102, 20240102, 2024โ€ฆ
## $ id              <int> 1101, 1102, 1103, 1104, 1108, 1109, 1110, 1201, 1203, โ€ฆ
## $ TSE.Sector      <int> 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, โ€ฆ
## $ name            <chr> "TCC", "ACC", "CHC", "UCC", "Lucky Cement", "HSINGTA",โ€ฆ
## $ Open.NTD.       <dbl> 32.5373, 37.2642, 17.7825, 26.0628, 14.1679, 16.1807, โ€ฆ
## $ High.NTD.       <dbl> 32.5373, 37.4442, 17.7825, 26.1505, 14.1679, 16.2696, โ€ฆ
## $ Low.NTD.        <dbl> 32.3038, 36.9492, 17.5953, 25.9750, 14.0343, 16.1362, โ€ฆ
## $ price           <dbl> 32.3972, 37.0392, 17.6421, 26.0628, 14.0788, 16.1807, โ€ฆ
## $ Volume.1000S.   <int> 14937, 6223, 171, 260, 442, 228, 57, 126, 48, 1849, 40โ€ฆ
## $ Amount.NTD1000. <int> 518751, 256522, 3240, 7736, 6992, 4159, 1075, 2409, 28โ€ฆ
## $ cap             <int> 262026, 145941, 14896, 19995, 6395, 6209, 10754, 9640,โ€ฆ
# Set data_long
data_long <- data_renamed %>%
  select(id, date, price) %>%
  mutate(
    # Change 'date' to Date format (assuming YYYY/MM/DD structure)
    date = ymd(date),
    # Change 'id' to text (character)
    id = as.character(id)
  )
# Print the data
glimpse(data_long)
## Rows: 337,347
## Columns: 3
## $ id    <chr> "1101", "1102", "1103", "1104", "1108", "1109", "1110", "1201", โ€ฆ
## $ date  <date> 2024-01-02, 2024-01-02, 2024-01-02, 2024-01-02, 2024-01-02, 202โ€ฆ
## $ price <dbl> 32.3972, 37.0392, 17.6421, 26.0628, 14.0788, 16.1807, 18.3336, 1โ€ฆ
# Convert the data from long to wide
data_spread <- data_long %>%
  spread(
    key = date,        
    value = price      
  )
# Print the data
head(data_spread)
##     id 2024-01-02 2024-01-03 2024-01-04 2024-01-05 2024-01-08 2024-01-09
## 1 1101    32.3972    31.9304    31.9304    32.0704    31.9771    31.7903
## 2 1102    37.0392    36.5892    37.0392    36.9942    37.1742    36.9042
## 3 1103    17.6421    17.5017    17.5017    17.5485    17.5953    17.5485
## 4 1104    26.0628    25.8873    25.5363    25.7995    25.7118    25.3607
## 5 1108    14.0788    14.0343    14.0788    14.0788    14.1679    13.8115
## 6 1109    16.1807    16.0918    16.1362    16.1362    16.0918    16.0473
##   2024-01-10 2024-01-11 2024-01-12 2024-01-15 2024-01-16 2024-01-17 2024-01-18
## 1    31.4636    31.4636    31.5103    31.4169    30.7167    30.0631    29.9231
## 2    36.6342    36.7242    36.5892    36.5892    35.9591    35.4190    35.3740
## 3    17.4081    17.5017    17.5017    17.4081    16.9870    16.8934    16.9402
## 4    25.3607    25.5801    25.6240    25.4485    25.0975    24.7903    24.8781
## 5    13.6778    13.7670    13.8115    13.7670    13.5442    13.4551    13.4551
## 6    16.0473    16.0918    16.0473    16.0029    15.8250    15.6472    15.6917
##   2024-01-19 2024-01-22 2024-01-23 2024-01-24 2024-01-25 2024-01-26 2024-01-29
## 1    30.2498    30.1565    30.5299    30.4366    30.5299    30.4366    30.2032
## 2    35.4640    35.5990    35.7791    35.9141    36.0041    36.0941    35.9591
## 3    16.8466    16.9402    16.8934    16.8934    16.8934    16.9870    16.9870
## 4    24.8342    25.0536    25.1852    25.4485    25.4046    25.3169    25.6240
## 5    13.4551    13.4996    13.5887    13.6333    13.5887    13.6333    13.7224
## 6    15.7806    15.9584    15.9139    15.9139    15.8695    15.8695    15.8695
##   2024-01-30 2024-01-31 2024-02-01 2024-02-02 2024-02-05 2024-02-15 2024-02-16
## 1    29.8764    29.7830    30.2965    30.1098    30.0164    29.8764    29.7830
## 2    35.8241    35.7791    36.0491    36.0491    35.5090    35.2840    35.6440
## 3    16.8934    16.7062    16.9402    16.9870    16.7998    16.7998    16.7062
## 4    25.6240    25.5801    25.7118    25.6679    25.4924    25.3607    25.4485
## 5    13.5887    13.5442    13.6778    13.5887    13.4551    13.6333    13.6333
## 6    15.8695    15.7361    15.7806    15.7806    15.7806    15.6472    15.6917
##   2024-02-19 2024-02-20 2024-02-21 2024-02-22 2024-02-23 2024-02-26 2024-02-27
## 1    30.6233    30.4833    30.3432    30.3899    30.2498    30.0164    29.7363
## 2    36.1391    36.1841    36.2741    36.3641    36.1841    36.2291    36.0941
## 3    16.7530    16.8934    16.8934    16.9402    16.8466    16.9402    16.7062
## 4    25.5801    25.3607    25.4046    25.5363    25.6240    25.7995    25.6679
## 5    14.0343    13.8561    13.8561    13.8561    13.8115    13.9006    13.8561
## 6    15.7806    15.7361    15.7361    15.7361    15.7361    15.7806    15.6917
##   2024-02-29 2024-03-01 2024-03-04 2024-03-05 2024-03-06 2024-03-07 2024-03-08
## 1    29.9698    29.9231    29.9231    29.8764    29.8764    29.8764    29.7363
## 2    36.4091    36.1391    36.0491    36.0491    36.0041    36.3191    36.3641
## 3    16.5658    16.6594    16.5658    16.4254    16.4722    16.3318    16.1914
## 4    26.0628    26.1066    26.8964    27.1158    27.3791    27.4230    27.2474
## 5    14.0343    14.0788    14.1234    14.3907    14.4798    14.3461    13.9452
## 6    15.8695    15.8250    15.7806    15.9139    15.9139    15.7361    15.6472
##   2024-03-11 2024-03-12 2024-03-13 2024-03-14 2024-03-15 2024-03-18 2024-03-19
## 1    29.6897    29.8764    29.5963    29.8764    29.8297    29.5963    29.3162
## 2    36.3641    36.5441    36.3191    37.0392    36.9942    36.9942    36.4991
## 3    16.2382    16.1914    16.1446    16.2382    16.0978    16.0510    16.0510
## 4    27.8178    27.5985    27.5546    27.9495    27.5985    28.5199    28.3882
## 5    14.0788    14.1234    13.9452    14.4353    14.5244    13.8561    13.9006
## 6    15.6028    15.6472    15.6917    16.6696    16.5807    16.9808    16.9364
##   2024-03-20 2024-03-21 2024-03-22 2024-03-25 2024-03-26 2024-03-27 2024-03-28
## 1    28.8494    29.4096    29.0828    29.0361    29.4563    29.5963    29.5963
## 2    36.6342    37.2192    37.1292    37.1742    37.4442    37.0842    36.8592
## 3    16.0042    16.0978    16.0042    16.0042    16.1914    16.1914    16.2850
## 4    28.0372    28.3005    27.9056    27.5546    27.2913    27.4668    27.6423
## 5    13.8561    14.1234    14.0343    14.0788    14.2125    14.2125    14.1234
## 6    16.8030    16.8474    16.8474    16.9364    17.1142    17.1586    17.0697
##   2024-03-29 2024-04-01 2024-04-02 2024-04-03 2024-04-08 2024-04-09 2024-04-10
## 1    30.0164    30.0164    29.8297    29.8764    30.1565    30.2965    30.5299
## 2    37.1742    37.2192    36.9942    37.0392    37.5793    37.8043    38.2543
## 3    16.2850    16.3786    16.2382    16.3318    16.4254    16.5190    16.7062
## 4    27.7301    28.0811    27.9056    28.1250    28.7393    29.3535    28.9148
## 5    14.0788    14.2125    14.0788    14.1679    14.8808    14.6580    14.7026
## 6    17.0697    17.1586    17.2475    17.2920    17.4698    17.5587    17.7809
##   2024-04-11 2024-04-12 2024-04-15 2024-04-16 2024-04-17 2024-04-18 2024-04-19
## 1    30.2498    30.0631    30.2032    30.2965    30.4833    30.5299    29.8764
## 2    37.8493    37.6243    37.8493    37.2642    37.3092    37.9393    37.8043
## 3    16.6126    16.4254    16.4722    16.1914    16.3318    16.6594    16.4254
## 4    28.6076    28.6515    28.7393    28.2566    29.0903    29.2219    29.0464
## 5    14.5244    14.5689    14.5244    14.3907    14.6580    15.1927    14.8808
## 6    17.2031    17.2031    17.2920    17.0697    17.1142    17.2475    17.0253
##   2024-04-22 2024-04-23 2024-04-24 2024-04-25 2024-04-26 2024-04-29 2024-04-30
## 1    30.4366    30.2032    30.0164    29.8764    29.6897    30.2032    29.9231
## 2    38.4344    38.6144    38.2093    38.2543    38.4794    39.2445    39.3345
## 3    16.6594    16.7530    16.6594    16.7062    16.7530    17.5485    17.2209
## 4    29.3096    29.8800    29.5729    29.7045    29.5290    30.8453    30.8453
## 5    15.1036    15.0590    15.0590    15.0590    14.9699    15.3263    15.1036
## 6    17.2475    17.3364    17.3364    17.2475    17.2031    17.6476    17.7809
##   2024-05-02 2024-05-03 2024-05-06 2024-05-07 2024-05-08 2024-05-09 2024-05-10
## 1    30.1565    30.2498    30.8100    30.9968    30.9034    30.4833    31.0901
## 2    39.4245    39.5595    40.0545    39.7845    39.5145    39.1094    41.4047
## 3    17.6421    17.3613    17.3145    17.2209    17.1741    17.0338    17.1273
## 4    31.0208    30.9331    31.0647    30.4943    30.1433    30.3188    30.6698
## 5    15.3709    15.2372    15.2818    15.2372    15.0590    15.0590    15.3263
## 6    18.6255    18.4033    18.3588    18.0032    17.8699    17.6476    18.0032
##   2024-05-13 2024-05-14 2024-05-15 2024-05-16 2024-05-17 2024-05-20 2024-05-21
## 1    31.4636    31.1368    31.0901    31.3702    31.6503    31.6036    31.1368
## 2    41.2247    41.0447    41.0447    41.1797    40.1446    40.9096    40.1446
## 3    17.1741    17.1273    17.0806    17.3145    17.2677    17.3613    17.1273
## 4    29.0464    28.9148    29.0903    29.0464    29.2219    29.8800    29.5729
## 5    15.2818    14.9253    15.1481    15.1927    15.1927    15.2372    15.0590
## 6    17.8699    17.7365    17.6920    17.7809    17.7809    17.7365    17.5587
##   2024-05-22 2024-05-23 2024-05-24 2024-05-27 2024-05-28 2024-05-29 2024-05-30
## 1    32.0704    31.7437    31.7903    31.5569    31.6970    30.9968    30.6700
## 2    39.5145    39.5595    39.6045    39.2445    39.3345    38.5244    37.8043
## 3    17.2209    16.9402    16.8934    16.7998    16.9870    16.9402    16.7530
## 4    29.5290    29.0464    29.1780    29.3096    29.3974    29.3974    29.2219
## 5    15.0590    14.7917    14.8362    14.9253    15.0590    15.0144    14.9699
## 6    17.6031    17.1586    17.2475    17.2920    17.4253    17.2475    17.2920
##   2024-05-31 2024-06-03 2024-06-04 2024-06-05 2024-06-06 2024-06-07 2024-06-11
## 1    30.8100    30.8100    30.5299    30.5299    30.5299    30.8567    30.6700
## 2    37.3992    37.4442    37.0842    37.0842    36.5441    37.2642    37.2642
## 3    17.7825    17.4549    17.1273    17.2677    17.0806    17.2209    16.9870
## 4    29.8362    29.7045    29.6168    29.7484    29.5290    29.7045    29.4413
## 5    15.1036    15.0590    15.0144    15.0144    15.0144    15.0590    15.0590
## 6    17.3364    17.3809    17.2475    17.3364    17.1142    17.2475    17.2031
##   2024-06-12 2024-06-13 2024-06-14 2024-06-17 2024-06-18 2024-06-19 2024-06-20
## 1    30.8100    31.2302    31.7437    31.7437    31.6503    31.7903    31.8837
## 2    37.3092    37.4893    37.6243    37.7593    37.9393    38.8394    39.8745
## 3    17.0338    17.0338    17.1273    17.1273    17.1273    16.9870    17.1741
## 4    29.5290    29.4852    29.5290    29.5729    29.6168    29.7923    30.0556
## 5    15.0144    14.9699    15.0590    14.9699    14.9253    14.9253    14.9699
## 6    17.1586    17.2031    17.3364    17.2920    17.2031    17.2475    17.2920
##   2024-06-21 2024-06-24 2024-06-25 2024-06-26 2024-06-27 2024-06-28 2024-07-01
## 1    32.3505    31.9771    32.2105    32.1171    31.9771    31.9304    32.2670
## 2    40.0995    40.3696    39.2445    39.1544    39.4695    39.4695    39.7845
## 3    17.2209    17.1741    17.2677    17.1741    17.1273    17.1273    17.2677
## 4    30.4943    30.5382    30.6698    30.8453    31.1086    31.2841    31.8106
## 5    15.0590    15.0590    15.0144    14.9699    14.8808    14.9253    14.9253
## 6    17.3809    17.3364    17.4698    17.3809    17.3809    17.5142    17.6031
##   2024-07-02 2024-07-03 2024-07-04 2024-07-05 2024-07-08 2024-07-09 2024-07-10
## 1    32.4113    32.6998    32.8441    33.1326    33.2769    32.6998    32.5555
## 2    39.6495    41.0897    41.3597    40.2796    40.2346    40.0995    39.9645
## 3    17.2209    17.3145    17.4549    17.5485    17.5017    17.3613    17.4549
## 4    31.7667    32.2494    32.3810    32.2494    32.0300    31.6790    31.4157
## 5    14.9699    15.1036    15.1481    15.1481    15.1481    14.7917    14.7917
## 6    17.5142    17.6476    17.6031    17.6920    17.7809    17.6031    17.6920
##   2024-07-11 2024-07-12 2024-07-15 2024-07-16 2024-07-17 2024-07-18 2024-07-19
## 1    32.7479    32.9402    32.7960    32.8441    33.1807    33.4692    33.2769
## 2    40.5496    41.1347    40.9096    40.3246    39.7845    39.4538    38.6978
## 3    17.5017    17.5017    17.5017    17.3613    17.4081    17.5043    17.2639
## 4    31.8106    31.3719    31.3719    30.9665    31.3436    31.3907    30.7780
## 5    14.7026    14.7917    14.7917    14.8362    14.8808    14.8362    14.6135
## 6    17.5587    17.5587    17.6476    17.6920    17.7365    17.5473    17.3108
##   2024-07-22 2024-07-23 2024-07-26 2024-07-29 2024-07-30 2024-07-31 2024-08-01
## 1    33.0364    33.6135    33.5654    33.5654    33.3249    33.3249    33.2769
## 2    38.6505    38.9340    38.7923    39.3120    39.0285    39.1703    38.9340
## 3    17.1677    17.3120    17.2639    17.4562    17.4081    17.3120    17.5524
## 4    29.8353    30.3066    30.5423    31.6735    31.4850    31.4378    31.7678
## 5    14.3907    14.5689    14.4798    14.4353    14.4798    14.5689    14.6135
## 6    17.0270    17.0743    17.0743    17.1216    17.0270    17.1216    17.3108
##   2024-08-02 2024-08-05 2024-08-06 2024-08-07 2024-08-08 2024-08-09 2024-08-12
## 1    32.9883    31.8342    32.5075    32.5075    32.6998    33.1807    33.1326
## 2    39.1703    38.1308    39.4065    39.3120    40.0208    40.9185    41.1075
## 3    17.2639    16.5425    16.7349    16.7349    16.5425    16.8311    17.0234
## 4    30.9194    28.3742    28.6570    29.3640    29.0341    28.8455    29.3640
## 5    14.4353    13.5442    13.6778    13.9006    13.8115    14.1679    14.1679
## 6    17.0270    16.1284    16.3176    16.5541    16.5068    16.5541    16.5068
##   2024-08-13 2024-08-14 2024-08-15 2024-08-16 2024-08-19 2024-08-20 2024-08-21
## 1    33.2769    32.3151    31.6419    31.5938    31.4495    31.5457    31.5457
## 2    41.2020    41.7690    41.2965    41.7218    41.9108    42.2415    41.8163
## 3    17.2639    17.1196    17.0715    17.0715    17.1196    17.2639    17.2639
## 4    28.8455    29.1283    28.7984    28.8927    28.8927    28.6570    28.9869
## 5    14.1679    14.2570    14.2570    14.3016    14.3461    14.3461    14.4798
## 6    16.5068    17.1689    17.0743    17.2162    17.2635    17.3581    17.3581
##   2024-08-22 2024-08-23 2024-08-26 2024-08-27 2024-08-28 2024-08-29 2024-08-30
## 1    31.7861    31.6900    31.8823    31.7861    31.6419    31.6900    31.6900
## 2    42.3360    42.4778    42.5250    42.4305    42.4305    42.4305    42.6195
## 3    17.2158    17.2158    17.3600    17.4562    17.4562    17.4562    17.6486
## 4    28.7041    28.7041    28.6570    28.6570    28.6570    28.7041    29.1283
## 5    14.4798    14.4798    14.5244    14.4798    14.4323    14.3848    14.4323
## 6    17.3581    17.4054    17.4527    17.4054    17.3581    17.3581    17.3581
##   2024-09-02 2024-09-03 2024-09-04 2024-09-05 2024-09-06 2024-09-09 2024-09-10
## 1    31.4976    30.9206    30.5358    30.3435    30.5358    30.3435    30.2473
## 2    42.6668    42.6195    41.8635    42.2415    42.4778    42.2888    42.3833
## 3    17.6005    17.6005    16.8311    17.0234    16.8311    16.5906    16.7349
## 4    28.7984    28.4685    27.6201    27.5258    27.5729    27.4315    27.4787
## 5    14.3374    14.3374    13.8152    13.8626    13.8152    13.7202    13.8626
## 6    17.4054    17.3581    17.0743    17.0743    17.2162    17.0743    17.0743
##   2024-09-11 2024-09-12 2024-09-13 2024-09-16 2024-09-18 2024-09-19 2024-09-20
## 1    30.1031    30.2954    30.5839    30.9686    30.8725    30.9686    31.0648
## 2    42.0525    42.2415    42.4305    42.5250    42.3360    42.4778    42.5250
## 3    16.5425    16.8311    17.0234    17.2639    17.3120    17.4081    16.7830
## 4    27.4787    27.9029    28.0914    28.4685    28.6099    28.7041    28.8927
## 5    13.7677    13.8626    13.8626    14.0051    14.0525    14.1950    14.1000
## 6    16.9797    17.1216    17.4527    17.5473    17.4527    17.5473    17.5946
##   2024-09-23 2024-09-24 2024-09-25 2024-09-26 2024-09-27 2024-09-30 2024-10-01
## 1    30.9686    31.0167    31.4976    31.3533    32.0266    32.5075    32.4113
## 2    42.6195    42.7140    43.2810    43.6590    44.0843    45.1238    44.8403
## 3    16.8792    17.0234    17.3600    17.3120    17.5043    17.5043    17.4562
## 4    28.6570    28.7041    28.9869    28.8927    28.9869    29.1283    28.8455
## 5    14.0525    14.1000    14.1475    14.1475    14.2424    14.3848    14.4323
## 6    17.5000    17.4527    17.4527    17.5473    17.7838    17.8311    17.7838
##   2024-10-04 2024-10-07 2024-10-08 2024-10-09 2024-10-11 2024-10-14 2024-10-15
## 1    32.5075    32.2189    31.7861    31.2091    31.2572    31.3053    31.2572
## 2    45.0293    44.7458    44.7458    44.0370    43.4700    43.4700    43.9898
## 3    17.2158    17.2639    17.1677    17.1196    17.0234    16.9753    16.8792
## 4    28.6099    29.2226    28.8455    28.5627    28.4685    28.3742    28.3271
## 5    14.1950    14.1475    13.9576    13.8152    13.7677    13.7677    13.7677
## 6    17.6419    17.7365    17.5473    17.5473    17.4527    17.4054    17.2162
##   2024-10-16 2024-10-17 2024-10-18 2024-10-21 2024-10-22 2024-10-23 2024-10-24
## 1    30.9206    31.2572    31.4976    31.0648    30.9206    30.9686    30.7763
## 2    43.1865    44.4150    44.9820    44.4150    44.3678    43.7535    44.2733
## 3    16.8311    16.9272    17.1196    17.1196    17.0234    16.8792    16.8792
## 4    29.7882    29.0812    28.8927    28.8455    28.9398    28.7984    28.6099
## 5    13.8152    13.6727    13.8152    13.8152    13.8152    13.8626    13.7677
## 6    17.4054    17.4054    17.4054    17.5473    17.5946    17.5000    17.5473
##   2024-10-25 2024-10-28 2024-10-29 2024-10-30 2024-11-01 2024-11-04 2024-11-05
## 1    30.8244    30.9206    30.8725    30.7282    30.8725    31.2091    31.3053
## 2    44.1788    44.1788    44.0370    44.0843    43.7535    44.5095    44.9820
## 3    16.9753    17.0234    16.8792    16.9272    17.0234    16.8311    16.9753
## 4    28.6099    28.8927    28.5156    28.5156    29.4111    28.8927    28.9398
## 5    13.8152    14.2899    14.0051    13.9101    14.0051    13.7202    13.7202
## 6    17.5000    17.5000    17.5000    17.4527    17.5946    17.5000    17.5473
##   2024-11-06 2024-11-07 2024-11-08 2024-11-11 2024-11-12 2024-11-13 2024-11-14
## 1    30.8244    30.7282    30.6801    31.0167    31.2091    31.2091    31.1129
## 2    44.4623    44.7930    44.9348    43.8953    43.5173    43.2338    43.0920
## 3    17.0715    17.1677    17.2639    17.3600    17.0715    17.1677    16.9753
## 4    28.8455    28.7984    28.2799    28.1857    27.8557    27.6201    27.3373
## 5    13.8152    13.8626    13.8152    13.7677    13.6727    13.7202    13.6727
## 6    17.5473    17.5473    17.6419    17.6419    17.4527    17.3581    17.2635
##   2024-11-15 2024-11-18 2024-11-19 2024-11-20 2024-11-21 2024-11-22 2024-11-25
## 1    31.8823    31.9785    32.2189    32.2189    31.9304    32.0747    32.6036
## 2    43.6590    42.7613    41.5800    41.3438    41.2965    41.4855    41.4855
## 3    17.2639    17.3120    17.4081    17.3120    17.3120    17.3120    17.5524
## 4    27.5258    27.6672    27.6201    27.4787    27.5729    27.7615    28.0914
## 5    13.7677    13.8152    13.7202    13.7202    13.7677    13.8152    13.8152
## 6    17.3108    17.2635    17.3108    17.3108    17.3108    17.4527    17.3581
##   2024-11-26 2024-11-27 2024-11-28 2024-11-29 2024-12-02 2024-12-03 2024-12-04
## 1    32.5075    32.1228    32.1228    32.2189    32.0266    32.5075    32.5075
## 2    40.9658    40.1625    39.9735    39.7373    40.5405    41.1075    40.0680
## 3    17.4562    17.3600    17.2639    17.2639    17.2158    17.4081    17.4081
## 4    27.9500    27.6201    27.6201    27.5729    27.5258    27.7143    27.9029
## 5    13.8152    13.7677    13.7202    13.7202    13.7677    13.7677    13.7677
## 6    17.3581    17.2162    17.3108    17.3581    17.4054    17.5473    17.4527
##   2024-12-05 2024-12-06 2024-12-09 2024-12-10 2024-12-11 2024-12-12 2024-12-13
## 1    32.3151    32.4113    32.2670    31.8823    32.0266    31.9304    31.5457
## 2    40.1153    40.3515    40.1625    39.9735    40.3515    40.4933    40.3988
## 3    17.4081    17.2158    17.2639    17.3120    17.3120    17.1196    17.0234
## 4    27.8557    27.6672    27.6201    27.5258    27.6201    27.2901    26.8188
## 5    13.7677    13.7677    13.8626    13.8626    13.8626    13.9576    13.8626
## 6    17.4054    17.5000    17.4527    17.5000    17.4527    17.4527    17.3581
##   2024-12-16 2024-12-17 2024-12-18 2024-12-19 2024-12-20 2024-12-23 2024-12-24
## 1    31.2091    31.1129    31.2572    30.9686    30.6320    31.0167    31.0167
## 2    39.3593    39.8790    39.9263    39.2648    39.1703    38.6978    38.4615
## 3    16.8792    16.7830    16.6868    16.3983    16.2059    16.4464    16.3983
## 4    26.2061    26.2061    26.2532    26.2061    26.1589    26.6303    26.3475
## 5    13.8152    13.7677    13.7677    13.6727    13.6253    13.7202    13.7677
## 6    17.1689    17.2162    17.1216    16.9797    16.9324    16.9797    16.9797
##   2024-12-25 2024-12-26 2024-12-27 2024-12-30 2024-12-31 2025-01-02 2025-01-03
## 1    31.0167    30.8725    30.6801    30.4878    30.4878    30.3916    30.2954
## 2    38.6505    38.7923    38.6033    38.3670    38.1780    38.0363    37.8945
## 3    16.4944    16.4944    16.3502    16.3983    16.3502    16.3502    16.1097
## 4    26.3475    27.4787    27.3373    27.2430    26.9602    26.6303    26.6303
## 5    13.7202    13.7202    13.7202    13.7202    13.7202    13.6727    13.5778
## 6    16.9797    16.9324    16.9324    16.8378    16.7432    16.6014    16.6014
##   2025-01-06 2025-01-07 2025-01-08 2025-01-09 2025-01-10 2025-01-13 2025-01-14
## 1    30.6320    30.2954    30.2473    29.6222    29.0932    29.3336    29.4298
## 2    38.1780    38.2725    37.6583    37.2803    37.0440    37.2803    37.6583
## 3    16.2540    16.0136    15.9655    15.8212    15.8212    15.4846    15.7731
## 4    26.7245    26.5831    26.7717    26.1118    25.9704    25.5462    25.9704
## 5    13.6727    13.6253    13.5303    13.4354    13.4354    13.3404    13.5303
## 6    16.7432    16.5068    16.4122    16.1284    16.1284    16.1757    16.1284
##   2025-01-15 2025-01-16 2025-01-17 2025-01-20 2025-01-21 2025-01-22 2025-02-03
## 1    29.6222    29.7664    30.4878    30.6320    30.5839    30.6320    30.5358
## 2    38.1308    38.6505    39.1230    38.8868    38.8395    38.8395    38.4615
## 3    15.9655    16.1097    16.2540    16.3983    16.5425    16.5906    16.5425
## 4    26.0647    26.1118    26.5831    26.4889    26.8188    26.8659    26.9131
## 5    13.5303    13.6253    13.5778    13.6253    13.6727    13.7202    13.6253
## 6    16.1757    16.2703    16.3176    16.3649    16.5068    16.4595    16.3176
##   2025-02-04 2025-02-05 2025-02-06 2025-02-07 2025-02-10 2025-02-11 2025-02-12
## 1    30.0550    29.9107    30.2954    30.0069    29.9588    30.2954    30.6801
## 2    38.5088    38.7450    38.6505    38.6033    37.8000    37.8000    37.8945
## 3    16.2059    16.3021    16.5425    16.4944    16.4464    16.4464    16.2540
## 4    26.4417    26.5360    26.6774    26.8188    26.4889    26.6303    26.8659
## 5    13.6253    13.7677    13.8152    13.8152    13.9101    13.9101    13.9576
## 6    16.2703    16.3649    16.4595    16.3649    16.3649    16.4122    16.4122
##   2025-02-13 2025-02-14 2025-02-17 2025-02-18 2025-02-19 2025-02-20 2025-02-21
## 1    32.1708    31.7861    32.2189    32.1708    31.7861    32.6517    32.8922
## 2    39.0758    38.9340    39.2175    39.1230    38.8395    39.2648    39.6900
## 3    16.6387    16.7349    16.6387    16.3502    16.3502    16.4944    16.5425
## 4    27.4787    27.7143    27.7615    27.5729    27.6672    27.8557    27.9500
## 5    14.1950    14.1475    14.1475    14.0525    14.0525    14.1000    14.1475
## 6    16.6014    16.7905    16.8378    16.8851    16.8378    16.9324    16.8851
##   2025-02-24 2025-02-25 2025-02-26 2025-02-27 2025-03-03 2025-03-04 2025-03-05
## 1    32.6998    33.6135    33.9020    33.7577    33.6616    33.2769    33.8058
## 2    39.7845    39.5955    39.7845    39.5010    41.6745    41.2965    41.1548
## 3    16.4944    16.7349    16.5906    15.7250    15.7731    15.8212    15.9174
## 4    27.9500    27.9971    28.2799    28.5627    28.9869    28.6570    28.7513
## 5    14.1475    14.2899    14.3374    14.2899    14.1950    14.2424    14.2424
## 6    16.8851    16.9324    16.8851    16.9797    16.9797    17.0743    17.0743
##   2025-03-06 2025-03-07 2025-03-10 2025-03-11 2025-03-12 2025-03-13 2025-03-14
## 1    33.4692    33.1807    33.4211    33.4692    33.1807    32.1228    32.5555
## 2    41.3438    41.5800    41.6273    41.7218    41.6745    41.6745    42.1470
## 3    16.0616    15.9174    15.9174    15.7250    15.7731    15.5327    15.7250
## 4    28.5627    28.2799    28.4213    28.4685    28.2799    28.1857    28.5156
## 5    14.2424    14.2424    14.2899    14.3848    14.4798    14.4798    14.4798
## 6    17.0270    16.9324    16.9797    16.8851    16.8378    16.8378    16.8851
##   2025-03-17 2025-03-18 2025-03-19 2025-03-20 2025-03-21 2025-03-24 2025-03-25
## 1    32.4113    32.3151    32.3632    32.4113    32.2189    31.8823    31.5457
## 2    41.8635    42.5250    42.5723    42.9975    42.6195    43.2338    43.3283
## 3    15.7250    15.8212    15.7250    15.7250    16.2540    15.7731    15.6769
## 4    28.4685    28.5156    28.6099    28.6099    28.3742    28.4685    28.5156
## 5    14.5273    14.1000    14.1950    14.3848    14.2899    14.2899    14.2424
## 6    16.9324    16.9797    17.0743    17.0270    16.8851    16.8378    16.9324
##   2025-03-26 2025-03-27 2025-03-28 2025-03-31 2025-04-01 2025-04-02 2025-04-07
## 1    31.7861    31.9785    31.5938    30.8244    31.1610    31.1610    28.6604
## 2    44.0370    44.5568    44.4150    44.1315    44.3678    44.6513    40.2098
## 3    15.6769    15.6769    15.4846    15.1960    15.3403    15.3884    13.8496
## 4    28.3742    28.3271    28.0914    27.5258    27.9029    27.9500    25.1691
## 5    14.2899    14.2424    14.0525    13.9576    14.1000    14.0051    12.6283
## 6    16.9324    16.8378    16.6959    16.5541    16.6486    16.6014    14.9459
##   2025-04-08 2025-04-09 2025-04-10 2025-04-11 2025-04-14 2025-04-15 2025-04-16
## 1    28.5162    27.4101    29.5741    28.8528    28.7566    28.7566    28.3719
## 2    41.4383    40.9185    42.6668    42.7613    42.8085    42.3833    41.6273
## 3    13.7053    13.0321    14.3304    14.2343    14.1862    14.3785    14.0900
## 4    25.2634    24.4150    26.5831    26.0175    25.6876    26.0647    25.6876
## 5    12.3434    12.1061    13.1505    13.1505    12.9131    13.1505    13.0556
## 6    15.0878    14.6622    15.7973    15.5608    15.4662    15.9392    15.9392
##   2025-04-17 2025-04-18 2025-04-21 2025-04-22 2025-04-23 2025-04-24 2025-04-25
## 1    28.2276    27.8429    27.6025    27.5063    28.1315    28.1315    28.2276
## 2    42.0525    42.0525    42.3833    41.8163    42.0053    42.0053    41.5328
## 3    14.1381    14.2343    14.0419    13.9938    14.1381    14.2343    14.3785
## 4    25.5933    25.8761    25.4048    25.4519    25.7819    25.9233    26.0175
## 5    13.1030    13.0556    13.0081    12.8657    12.9606    12.9606    13.0556
## 6    15.8446    15.7500    15.6554    15.4662    15.4662    15.4189    15.4189
##   2025-04-28 2025-04-29 2025-04-30 2025-05-02 2025-05-05 2025-05-06 2025-05-07
## 1    28.2276    28.3719    28.5162    28.8047    29.4298    29.3817    29.6703
## 2    42.1943    42.4305    42.5250    42.3833    43.0448    43.1393    43.1865
## 3    14.3304    14.4266    14.2824    14.5709    15.0037    15.2441    15.3403
## 4    26.3475    26.6303    26.3475    26.6774    26.8188    27.0073    27.0545
## 5    13.1030    13.2455    13.1505    13.8152    14.2899    14.2424    14.1475
## 6    15.6081    15.8446    15.8919    16.0338    16.4595    16.5541    16.6486
##   2025-05-08 2025-05-09 2025-05-12 2025-05-13 2025-05-14 2025-05-15 2025-05-16
## 1    29.3817    29.5741    30.0069    29.9107    29.1894    28.9489    28.9489
## 2    42.5723    43.0448    42.6195    42.7613    42.4305    41.8163    41.9580
## 3    15.1480    15.1960    15.1480    15.1960    15.1480    15.0999    15.1960
## 4    27.4315    27.6201    27.9971    27.4787    27.5729    27.4787    27.7615
## 5    14.1950    14.1950    14.3374    14.3848    14.5273    14.5273    14.4798
## 6    16.2703    16.3176    16.3176    16.2230    16.4122    16.2703    16.3176
##   2025-05-19 2025-05-20 2025-05-21 2025-05-22 2025-05-23 2025-05-26 2025-05-27
## 1    28.6604    28.9970    29.1413    28.8528    28.6123    28.7085    28.2276
## 2    41.7690    42.6195    42.3360    40.9658    40.5878    40.3515    39.6900
## 3    15.2441    15.2922    15.5327    15.3884    15.2922    15.0518    14.7632
## 4    27.7143    27.8557    28.0914    27.9971    28.7513    28.9869    29.0812
## 5    14.4798    14.5273    14.4798    14.5273    14.4798    14.4798    14.4323
## 6    16.2230    16.4595    16.5068    16.4122    16.3176    16.5541    16.3649
##   2025-05-28 2025-05-29 2025-06-02 2025-06-03 2025-06-04 2025-06-05 2025-06-06
## 1    27.9872    28.2276    26.6407    25.8232    25.9675    26.4484    26.4965
## 2    40.0208    39.5483    39.4065    39.5010    39.1230    39.3593    39.7845
## 3    14.5709    14.5709    14.1381    14.0900    14.2343    14.3785    14.5228
## 4    28.8455    29.1283    28.3271    28.4685    29.3169    28.7513    28.5156
## 5    14.4323    14.4798    14.3374    14.2899    14.3374    14.3374    14.2899
## 6    16.3176    16.4122    16.0811    15.8919    15.9865    16.1757    16.1284
##   2025-06-09 2025-06-10 2025-06-11 2025-06-12 2025-06-13 2025-06-16 2025-06-17
## 1    26.1598    26.1118    26.2560    26.0156    25.8713    25.7751    25.7271
## 2    39.8790    40.4460    40.7768    40.4460    41.0130    40.5405    40.4460
## 3    14.4747    14.4266    14.1381    14.1862    13.9457    13.9938    14.0419
## 4    28.3742    28.4685    28.3742    28.2799    28.1857    28.4213    28.4213
## 5    14.2424    14.1950    14.1950    14.1950    14.1475    14.1475    14.1000
## 6    16.1284    16.1284    16.1284    16.0338    15.9392    16.0811    16.0811
##   2025-06-18 2025-06-19 2025-06-20 2025-06-23 2025-06-24 2025-06-25 2025-06-26
## 1    25.4866    25.0538    24.5249    24.1402    24.7653    24.7172    24.8134
## 2    40.1153    39.5483    39.5483    39.5955    39.9735    39.9263    40.3043
## 3    13.9938    13.7534    13.5129    13.5129    13.8496    13.8976    14.0900
## 4    28.7041    28.1857    28.5627    27.9971    27.9500    27.9000    28.1500
## 5    14.1475    14.0051    13.9576    13.7677    14.1475    14.1000    14.1475
## 6    15.9865    15.8446    15.7973    15.7027    15.8919    15.9865    15.9392
##   2025-06-27 2025-06-30
## 1    25.1981    24.5249
## 2    40.8240    40.3043
## 3    14.1381    14.0900
## 4    28.1000    27.8500
## 5    14.1000    14.0525
## 6    15.9865    15.9392
na_counts_by_id <- data_spread %>%
  # Treat each row (stock ID) as a separate group
  rowwise() %>%
  
  # Calculate the number of NA values across all date columns.
  # The -id part tells it to check every column EXCEPT the 'id' column.
  mutate(
    NA_count = sum(is.na(c_across(-id))) 
  ) %>%
  
  # Filter to keep only stock IDs that have at least one NA value
  filter(NA_count > 0) %>%
  
  # Select the stock ID and its total NA count
  select(id, NA_count) %>%
  
  # Ungroup for clean output (optional, but good practice)
  ungroup() %>%
  
  # Arrange by the number of NA counts (descending)
  arrange(desc(NA_count))

# Show the results
print(head(na_counts_by_id))
## # A tibble: 6 ร— 2
##   id    NA_count
##   <chr>    <int>
## 1 7799       217
## 2 7788       198
## 3 7780       196
## 4 4585       177
## 5 3716       160
## 6 7765       151
data_filled <- data_spread %>%
  # Use mutate(across(...)) to apply na.locf to specific columns.
  # We select all columns EXCEPT the 'id' column.
  mutate(
    across(-id, ~na.locf(., na.rm = FALSE))
  )
head(data_filled)
##     id 2024-01-02 2024-01-03 2024-01-04 2024-01-05 2024-01-08 2024-01-09
## 1 1101    32.3972    31.9304    31.9304    32.0704    31.9771    31.7903
## 2 1102    37.0392    36.5892    37.0392    36.9942    37.1742    36.9042
## 3 1103    17.6421    17.5017    17.5017    17.5485    17.5953    17.5485
## 4 1104    26.0628    25.8873    25.5363    25.7995    25.7118    25.3607
## 5 1108    14.0788    14.0343    14.0788    14.0788    14.1679    13.8115
## 6 1109    16.1807    16.0918    16.1362    16.1362    16.0918    16.0473
##   2024-01-10 2024-01-11 2024-01-12 2024-01-15 2024-01-16 2024-01-17 2024-01-18
## 1    31.4636    31.4636    31.5103    31.4169    30.7167    30.0631    29.9231
## 2    36.6342    36.7242    36.5892    36.5892    35.9591    35.4190    35.3740
## 3    17.4081    17.5017    17.5017    17.4081    16.9870    16.8934    16.9402
## 4    25.3607    25.5801    25.6240    25.4485    25.0975    24.7903    24.8781
## 5    13.6778    13.7670    13.8115    13.7670    13.5442    13.4551    13.4551
## 6    16.0473    16.0918    16.0473    16.0029    15.8250    15.6472    15.6917
##   2024-01-19 2024-01-22 2024-01-23 2024-01-24 2024-01-25 2024-01-26 2024-01-29
## 1    30.2498    30.1565    30.5299    30.4366    30.5299    30.4366    30.2032
## 2    35.4640    35.5990    35.7791    35.9141    36.0041    36.0941    35.9591
## 3    16.8466    16.9402    16.8934    16.8934    16.8934    16.9870    16.9870
## 4    24.8342    25.0536    25.1852    25.4485    25.4046    25.3169    25.6240
## 5    13.4551    13.4996    13.5887    13.6333    13.5887    13.6333    13.7224
## 6    15.7806    15.9584    15.9139    15.9139    15.8695    15.8695    15.8695
##   2024-01-30 2024-01-31 2024-02-01 2024-02-02 2024-02-05 2024-02-15 2024-02-16
## 1    29.8764    29.7830    30.2965    30.1098    30.0164    29.8764    29.7830
## 2    35.8241    35.7791    36.0491    36.0491    35.5090    35.2840    35.6440
## 3    16.8934    16.7062    16.9402    16.9870    16.7998    16.7998    16.7062
## 4    25.6240    25.5801    25.7118    25.6679    25.4924    25.3607    25.4485
## 5    13.5887    13.5442    13.6778    13.5887    13.4551    13.6333    13.6333
## 6    15.8695    15.7361    15.7806    15.7806    15.7806    15.6472    15.6917
##   2024-02-19 2024-02-20 2024-02-21 2024-02-22 2024-02-23 2024-02-26 2024-02-27
## 1    30.6233    30.4833    30.3432    30.3899    30.2498    30.0164    29.7363
## 2    36.1391    36.1841    36.2741    36.3641    36.1841    36.2291    36.0941
## 3    16.7530    16.8934    16.8934    16.9402    16.8466    16.9402    16.7062
## 4    25.5801    25.3607    25.4046    25.5363    25.6240    25.7995    25.6679
## 5    14.0343    13.8561    13.8561    13.8561    13.8115    13.9006    13.8561
## 6    15.7806    15.7361    15.7361    15.7361    15.7361    15.7806    15.6917
##   2024-02-29 2024-03-01 2024-03-04 2024-03-05 2024-03-06 2024-03-07 2024-03-08
## 1    29.9698    29.9231    29.9231    29.8764    29.8764    29.8764    29.7363
## 2    36.4091    36.1391    36.0491    36.0491    36.0041    36.3191    36.3641
## 3    16.5658    16.6594    16.5658    16.4254    16.4722    16.3318    16.1914
## 4    26.0628    26.1066    26.8964    27.1158    27.3791    27.4230    27.2474
## 5    14.0343    14.0788    14.1234    14.3907    14.4798    14.3461    13.9452
## 6    15.8695    15.8250    15.7806    15.9139    15.9139    15.7361    15.6472
##   2024-03-11 2024-03-12 2024-03-13 2024-03-14 2024-03-15 2024-03-18 2024-03-19
## 1    29.6897    29.8764    29.5963    29.8764    29.8297    29.5963    29.3162
## 2    36.3641    36.5441    36.3191    37.0392    36.9942    36.9942    36.4991
## 3    16.2382    16.1914    16.1446    16.2382    16.0978    16.0510    16.0510
## 4    27.8178    27.5985    27.5546    27.9495    27.5985    28.5199    28.3882
## 5    14.0788    14.1234    13.9452    14.4353    14.5244    13.8561    13.9006
## 6    15.6028    15.6472    15.6917    16.6696    16.5807    16.9808    16.9364
##   2024-03-20 2024-03-21 2024-03-22 2024-03-25 2024-03-26 2024-03-27 2024-03-28
## 1    28.8494    29.4096    29.0828    29.0361    29.4563    29.5963    29.5963
## 2    36.6342    37.2192    37.1292    37.1742    37.4442    37.0842    36.8592
## 3    16.0042    16.0978    16.0042    16.0042    16.1914    16.1914    16.2850
## 4    28.0372    28.3005    27.9056    27.5546    27.2913    27.4668    27.6423
## 5    13.8561    14.1234    14.0343    14.0788    14.2125    14.2125    14.1234
## 6    16.8030    16.8474    16.8474    16.9364    17.1142    17.1586    17.0697
##   2024-03-29 2024-04-01 2024-04-02 2024-04-03 2024-04-08 2024-04-09 2024-04-10
## 1    30.0164    30.0164    29.8297    29.8764    30.1565    30.2965    30.5299
## 2    37.1742    37.2192    36.9942    37.0392    37.5793    37.8043    38.2543
## 3    16.2850    16.3786    16.2382    16.3318    16.4254    16.5190    16.7062
## 4    27.7301    28.0811    27.9056    28.1250    28.7393    29.3535    28.9148
## 5    14.0788    14.2125    14.0788    14.1679    14.8808    14.6580    14.7026
## 6    17.0697    17.1586    17.2475    17.2920    17.4698    17.5587    17.7809
##   2024-04-11 2024-04-12 2024-04-15 2024-04-16 2024-04-17 2024-04-18 2024-04-19
## 1    30.2498    30.0631    30.2032    30.2965    30.4833    30.5299    29.8764
## 2    37.8493    37.6243    37.8493    37.2642    37.3092    37.9393    37.8043
## 3    16.6126    16.4254    16.4722    16.1914    16.3318    16.6594    16.4254
## 4    28.6076    28.6515    28.7393    28.2566    29.0903    29.2219    29.0464
## 5    14.5244    14.5689    14.5244    14.3907    14.6580    15.1927    14.8808
## 6    17.2031    17.2031    17.2920    17.0697    17.1142    17.2475    17.0253
##   2024-04-22 2024-04-23 2024-04-24 2024-04-25 2024-04-26 2024-04-29 2024-04-30
## 1    30.4366    30.2032    30.0164    29.8764    29.6897    30.2032    29.9231
## 2    38.4344    38.6144    38.2093    38.2543    38.4794    39.2445    39.3345
## 3    16.6594    16.7530    16.6594    16.7062    16.7530    17.5485    17.2209
## 4    29.3096    29.8800    29.5729    29.7045    29.5290    30.8453    30.8453
## 5    15.1036    15.0590    15.0590    15.0590    14.9699    15.3263    15.1036
## 6    17.2475    17.3364    17.3364    17.2475    17.2031    17.6476    17.7809
##   2024-05-02 2024-05-03 2024-05-06 2024-05-07 2024-05-08 2024-05-09 2024-05-10
## 1    30.1565    30.2498    30.8100    30.9968    30.9034    30.4833    31.0901
## 2    39.4245    39.5595    40.0545    39.7845    39.5145    39.1094    41.4047
## 3    17.6421    17.3613    17.3145    17.2209    17.1741    17.0338    17.1273
## 4    31.0208    30.9331    31.0647    30.4943    30.1433    30.3188    30.6698
## 5    15.3709    15.2372    15.2818    15.2372    15.0590    15.0590    15.3263
## 6    18.6255    18.4033    18.3588    18.0032    17.8699    17.6476    18.0032
##   2024-05-13 2024-05-14 2024-05-15 2024-05-16 2024-05-17 2024-05-20 2024-05-21
## 1    31.4636    31.1368    31.0901    31.3702    31.6503    31.6036    31.1368
## 2    41.2247    41.0447    41.0447    41.1797    40.1446    40.9096    40.1446
## 3    17.1741    17.1273    17.0806    17.3145    17.2677    17.3613    17.1273
## 4    29.0464    28.9148    29.0903    29.0464    29.2219    29.8800    29.5729
## 5    15.2818    14.9253    15.1481    15.1927    15.1927    15.2372    15.0590
## 6    17.8699    17.7365    17.6920    17.7809    17.7809    17.7365    17.5587
##   2024-05-22 2024-05-23 2024-05-24 2024-05-27 2024-05-28 2024-05-29 2024-05-30
## 1    32.0704    31.7437    31.7903    31.5569    31.6970    30.9968    30.6700
## 2    39.5145    39.5595    39.6045    39.2445    39.3345    38.5244    37.8043
## 3    17.2209    16.9402    16.8934    16.7998    16.9870    16.9402    16.7530
## 4    29.5290    29.0464    29.1780    29.3096    29.3974    29.3974    29.2219
## 5    15.0590    14.7917    14.8362    14.9253    15.0590    15.0144    14.9699
## 6    17.6031    17.1586    17.2475    17.2920    17.4253    17.2475    17.2920
##   2024-05-31 2024-06-03 2024-06-04 2024-06-05 2024-06-06 2024-06-07 2024-06-11
## 1    30.8100    30.8100    30.5299    30.5299    30.5299    30.8567    30.6700
## 2    37.3992    37.4442    37.0842    37.0842    36.5441    37.2642    37.2642
## 3    17.7825    17.4549    17.1273    17.2677    17.0806    17.2209    16.9870
## 4    29.8362    29.7045    29.6168    29.7484    29.5290    29.7045    29.4413
## 5    15.1036    15.0590    15.0144    15.0144    15.0144    15.0590    15.0590
## 6    17.3364    17.3809    17.2475    17.3364    17.1142    17.2475    17.2031
##   2024-06-12 2024-06-13 2024-06-14 2024-06-17 2024-06-18 2024-06-19 2024-06-20
## 1    30.8100    31.2302    31.7437    31.7437    31.6503    31.7903    31.8837
## 2    37.3092    37.4893    37.6243    37.7593    37.9393    38.8394    39.8745
## 3    17.0338    17.0338    17.1273    17.1273    17.1273    16.9870    17.1741
## 4    29.5290    29.4852    29.5290    29.5729    29.6168    29.7923    30.0556
## 5    15.0144    14.9699    15.0590    14.9699    14.9253    14.9253    14.9699
## 6    17.1586    17.2031    17.3364    17.2920    17.2031    17.2475    17.2920
##   2024-06-21 2024-06-24 2024-06-25 2024-06-26 2024-06-27 2024-06-28 2024-07-01
## 1    32.3505    31.9771    32.2105    32.1171    31.9771    31.9304    32.2670
## 2    40.0995    40.3696    39.2445    39.1544    39.4695    39.4695    39.7845
## 3    17.2209    17.1741    17.2677    17.1741    17.1273    17.1273    17.2677
## 4    30.4943    30.5382    30.6698    30.8453    31.1086    31.2841    31.8106
## 5    15.0590    15.0590    15.0144    14.9699    14.8808    14.9253    14.9253
## 6    17.3809    17.3364    17.4698    17.3809    17.3809    17.5142    17.6031
##   2024-07-02 2024-07-03 2024-07-04 2024-07-05 2024-07-08 2024-07-09 2024-07-10
## 1    32.4113    32.6998    32.8441    33.1326    33.2769    32.6998    32.5555
## 2    39.6495    41.0897    41.3597    40.2796    40.2346    40.0995    39.9645
## 3    17.2209    17.3145    17.4549    17.5485    17.5017    17.3613    17.4549
## 4    31.7667    32.2494    32.3810    32.2494    32.0300    31.6790    31.4157
## 5    14.9699    15.1036    15.1481    15.1481    15.1481    14.7917    14.7917
## 6    17.5142    17.6476    17.6031    17.6920    17.7809    17.6031    17.6920
##   2024-07-11 2024-07-12 2024-07-15 2024-07-16 2024-07-17 2024-07-18 2024-07-19
## 1    32.7479    32.9402    32.7960    32.8441    33.1807    33.4692    33.2769
## 2    40.5496    41.1347    40.9096    40.3246    39.7845    39.4538    38.6978
## 3    17.5017    17.5017    17.5017    17.3613    17.4081    17.5043    17.2639
## 4    31.8106    31.3719    31.3719    30.9665    31.3436    31.3907    30.7780
## 5    14.7026    14.7917    14.7917    14.8362    14.8808    14.8362    14.6135
## 6    17.5587    17.5587    17.6476    17.6920    17.7365    17.5473    17.3108
##   2024-07-22 2024-07-23 2024-07-26 2024-07-29 2024-07-30 2024-07-31 2024-08-01
## 1    33.0364    33.6135    33.5654    33.5654    33.3249    33.3249    33.2769
## 2    38.6505    38.9340    38.7923    39.3120    39.0285    39.1703    38.9340
## 3    17.1677    17.3120    17.2639    17.4562    17.4081    17.3120    17.5524
## 4    29.8353    30.3066    30.5423    31.6735    31.4850    31.4378    31.7678
## 5    14.3907    14.5689    14.4798    14.4353    14.4798    14.5689    14.6135
## 6    17.0270    17.0743    17.0743    17.1216    17.0270    17.1216    17.3108
##   2024-08-02 2024-08-05 2024-08-06 2024-08-07 2024-08-08 2024-08-09 2024-08-12
## 1    32.9883    31.8342    32.5075    32.5075    32.6998    33.1807    33.1326
## 2    39.1703    38.1308    39.4065    39.3120    40.0208    40.9185    41.1075
## 3    17.2639    16.5425    16.7349    16.7349    16.5425    16.8311    17.0234
## 4    30.9194    28.3742    28.6570    29.3640    29.0341    28.8455    29.3640
## 5    14.4353    13.5442    13.6778    13.9006    13.8115    14.1679    14.1679
## 6    17.0270    16.1284    16.3176    16.5541    16.5068    16.5541    16.5068
##   2024-08-13 2024-08-14 2024-08-15 2024-08-16 2024-08-19 2024-08-20 2024-08-21
## 1    33.2769    32.3151    31.6419    31.5938    31.4495    31.5457    31.5457
## 2    41.2020    41.7690    41.2965    41.7218    41.9108    42.2415    41.8163
## 3    17.2639    17.1196    17.0715    17.0715    17.1196    17.2639    17.2639
## 4    28.8455    29.1283    28.7984    28.8927    28.8927    28.6570    28.9869
## 5    14.1679    14.2570    14.2570    14.3016    14.3461    14.3461    14.4798
## 6    16.5068    17.1689    17.0743    17.2162    17.2635    17.3581    17.3581
##   2024-08-22 2024-08-23 2024-08-26 2024-08-27 2024-08-28 2024-08-29 2024-08-30
## 1    31.7861    31.6900    31.8823    31.7861    31.6419    31.6900    31.6900
## 2    42.3360    42.4778    42.5250    42.4305    42.4305    42.4305    42.6195
## 3    17.2158    17.2158    17.3600    17.4562    17.4562    17.4562    17.6486
## 4    28.7041    28.7041    28.6570    28.6570    28.6570    28.7041    29.1283
## 5    14.4798    14.4798    14.5244    14.4798    14.4323    14.3848    14.4323
## 6    17.3581    17.4054    17.4527    17.4054    17.3581    17.3581    17.3581
##   2024-09-02 2024-09-03 2024-09-04 2024-09-05 2024-09-06 2024-09-09 2024-09-10
## 1    31.4976    30.9206    30.5358    30.3435    30.5358    30.3435    30.2473
## 2    42.6668    42.6195    41.8635    42.2415    42.4778    42.2888    42.3833
## 3    17.6005    17.6005    16.8311    17.0234    16.8311    16.5906    16.7349
## 4    28.7984    28.4685    27.6201    27.5258    27.5729    27.4315    27.4787
## 5    14.3374    14.3374    13.8152    13.8626    13.8152    13.7202    13.8626
## 6    17.4054    17.3581    17.0743    17.0743    17.2162    17.0743    17.0743
##   2024-09-11 2024-09-12 2024-09-13 2024-09-16 2024-09-18 2024-09-19 2024-09-20
## 1    30.1031    30.2954    30.5839    30.9686    30.8725    30.9686    31.0648
## 2    42.0525    42.2415    42.4305    42.5250    42.3360    42.4778    42.5250
## 3    16.5425    16.8311    17.0234    17.2639    17.3120    17.4081    16.7830
## 4    27.4787    27.9029    28.0914    28.4685    28.6099    28.7041    28.8927
## 5    13.7677    13.8626    13.8626    14.0051    14.0525    14.1950    14.1000
## 6    16.9797    17.1216    17.4527    17.5473    17.4527    17.5473    17.5946
##   2024-09-23 2024-09-24 2024-09-25 2024-09-26 2024-09-27 2024-09-30 2024-10-01
## 1    30.9686    31.0167    31.4976    31.3533    32.0266    32.5075    32.4113
## 2    42.6195    42.7140    43.2810    43.6590    44.0843    45.1238    44.8403
## 3    16.8792    17.0234    17.3600    17.3120    17.5043    17.5043    17.4562
## 4    28.6570    28.7041    28.9869    28.8927    28.9869    29.1283    28.8455
## 5    14.0525    14.1000    14.1475    14.1475    14.2424    14.3848    14.4323
## 6    17.5000    17.4527    17.4527    17.5473    17.7838    17.8311    17.7838
##   2024-10-04 2024-10-07 2024-10-08 2024-10-09 2024-10-11 2024-10-14 2024-10-15
## 1    32.5075    32.2189    31.7861    31.2091    31.2572    31.3053    31.2572
## 2    45.0293    44.7458    44.7458    44.0370    43.4700    43.4700    43.9898
## 3    17.2158    17.2639    17.1677    17.1196    17.0234    16.9753    16.8792
## 4    28.6099    29.2226    28.8455    28.5627    28.4685    28.3742    28.3271
## 5    14.1950    14.1475    13.9576    13.8152    13.7677    13.7677    13.7677
## 6    17.6419    17.7365    17.5473    17.5473    17.4527    17.4054    17.2162
##   2024-10-16 2024-10-17 2024-10-18 2024-10-21 2024-10-22 2024-10-23 2024-10-24
## 1    30.9206    31.2572    31.4976    31.0648    30.9206    30.9686    30.7763
## 2    43.1865    44.4150    44.9820    44.4150    44.3678    43.7535    44.2733
## 3    16.8311    16.9272    17.1196    17.1196    17.0234    16.8792    16.8792
## 4    29.7882    29.0812    28.8927    28.8455    28.9398    28.7984    28.6099
## 5    13.8152    13.6727    13.8152    13.8152    13.8152    13.8626    13.7677
## 6    17.4054    17.4054    17.4054    17.5473    17.5946    17.5000    17.5473
##   2024-10-25 2024-10-28 2024-10-29 2024-10-30 2024-11-01 2024-11-04 2024-11-05
## 1    30.8244    30.9206    30.8725    30.7282    30.8725    31.2091    31.3053
## 2    44.1788    44.1788    44.0370    44.0843    43.7535    44.5095    44.9820
## 3    16.9753    17.0234    16.8792    16.9272    17.0234    16.8311    16.9753
## 4    28.6099    28.8927    28.5156    28.5156    29.4111    28.8927    28.9398
## 5    13.8152    14.2899    14.0051    13.9101    14.0051    13.7202    13.7202
## 6    17.5000    17.5000    17.5000    17.4527    17.5946    17.5000    17.5473
##   2024-11-06 2024-11-07 2024-11-08 2024-11-11 2024-11-12 2024-11-13 2024-11-14
## 1    30.8244    30.7282    30.6801    31.0167    31.2091    31.2091    31.1129
## 2    44.4623    44.7930    44.9348    43.8953    43.5173    43.2338    43.0920
## 3    17.0715    17.1677    17.2639    17.3600    17.0715    17.1677    16.9753
## 4    28.8455    28.7984    28.2799    28.1857    27.8557    27.6201    27.3373
## 5    13.8152    13.8626    13.8152    13.7677    13.6727    13.7202    13.6727
## 6    17.5473    17.5473    17.6419    17.6419    17.4527    17.3581    17.2635
##   2024-11-15 2024-11-18 2024-11-19 2024-11-20 2024-11-21 2024-11-22 2024-11-25
## 1    31.8823    31.9785    32.2189    32.2189    31.9304    32.0747    32.6036
## 2    43.6590    42.7613    41.5800    41.3438    41.2965    41.4855    41.4855
## 3    17.2639    17.3120    17.4081    17.3120    17.3120    17.3120    17.5524
## 4    27.5258    27.6672    27.6201    27.4787    27.5729    27.7615    28.0914
## 5    13.7677    13.8152    13.7202    13.7202    13.7677    13.8152    13.8152
## 6    17.3108    17.2635    17.3108    17.3108    17.3108    17.4527    17.3581
##   2024-11-26 2024-11-27 2024-11-28 2024-11-29 2024-12-02 2024-12-03 2024-12-04
## 1    32.5075    32.1228    32.1228    32.2189    32.0266    32.5075    32.5075
## 2    40.9658    40.1625    39.9735    39.7373    40.5405    41.1075    40.0680
## 3    17.4562    17.3600    17.2639    17.2639    17.2158    17.4081    17.4081
## 4    27.9500    27.6201    27.6201    27.5729    27.5258    27.7143    27.9029
## 5    13.8152    13.7677    13.7202    13.7202    13.7677    13.7677    13.7677
## 6    17.3581    17.2162    17.3108    17.3581    17.4054    17.5473    17.4527
##   2024-12-05 2024-12-06 2024-12-09 2024-12-10 2024-12-11 2024-12-12 2024-12-13
## 1    32.3151    32.4113    32.2670    31.8823    32.0266    31.9304    31.5457
## 2    40.1153    40.3515    40.1625    39.9735    40.3515    40.4933    40.3988
## 3    17.4081    17.2158    17.2639    17.3120    17.3120    17.1196    17.0234
## 4    27.8557    27.6672    27.6201    27.5258    27.6201    27.2901    26.8188
## 5    13.7677    13.7677    13.8626    13.8626    13.8626    13.9576    13.8626
## 6    17.4054    17.5000    17.4527    17.5000    17.4527    17.4527    17.3581
##   2024-12-16 2024-12-17 2024-12-18 2024-12-19 2024-12-20 2024-12-23 2024-12-24
## 1    31.2091    31.1129    31.2572    30.9686    30.6320    31.0167    31.0167
## 2    39.3593    39.8790    39.9263    39.2648    39.1703    38.6978    38.4615
## 3    16.8792    16.7830    16.6868    16.3983    16.2059    16.4464    16.3983
## 4    26.2061    26.2061    26.2532    26.2061    26.1589    26.6303    26.3475
## 5    13.8152    13.7677    13.7677    13.6727    13.6253    13.7202    13.7677
## 6    17.1689    17.2162    17.1216    16.9797    16.9324    16.9797    16.9797
##   2024-12-25 2024-12-26 2024-12-27 2024-12-30 2024-12-31 2025-01-02 2025-01-03
## 1    31.0167    30.8725    30.6801    30.4878    30.4878    30.3916    30.2954
## 2    38.6505    38.7923    38.6033    38.3670    38.1780    38.0363    37.8945
## 3    16.4944    16.4944    16.3502    16.3983    16.3502    16.3502    16.1097
## 4    26.3475    27.4787    27.3373    27.2430    26.9602    26.6303    26.6303
## 5    13.7202    13.7202    13.7202    13.7202    13.7202    13.6727    13.5778
## 6    16.9797    16.9324    16.9324    16.8378    16.7432    16.6014    16.6014
##   2025-01-06 2025-01-07 2025-01-08 2025-01-09 2025-01-10 2025-01-13 2025-01-14
## 1    30.6320    30.2954    30.2473    29.6222    29.0932    29.3336    29.4298
## 2    38.1780    38.2725    37.6583    37.2803    37.0440    37.2803    37.6583
## 3    16.2540    16.0136    15.9655    15.8212    15.8212    15.4846    15.7731
## 4    26.7245    26.5831    26.7717    26.1118    25.9704    25.5462    25.9704
## 5    13.6727    13.6253    13.5303    13.4354    13.4354    13.3404    13.5303
## 6    16.7432    16.5068    16.4122    16.1284    16.1284    16.1757    16.1284
##   2025-01-15 2025-01-16 2025-01-17 2025-01-20 2025-01-21 2025-01-22 2025-02-03
## 1    29.6222    29.7664    30.4878    30.6320    30.5839    30.6320    30.5358
## 2    38.1308    38.6505    39.1230    38.8868    38.8395    38.8395    38.4615
## 3    15.9655    16.1097    16.2540    16.3983    16.5425    16.5906    16.5425
## 4    26.0647    26.1118    26.5831    26.4889    26.8188    26.8659    26.9131
## 5    13.5303    13.6253    13.5778    13.6253    13.6727    13.7202    13.6253
## 6    16.1757    16.2703    16.3176    16.3649    16.5068    16.4595    16.3176
##   2025-02-04 2025-02-05 2025-02-06 2025-02-07 2025-02-10 2025-02-11 2025-02-12
## 1    30.0550    29.9107    30.2954    30.0069    29.9588    30.2954    30.6801
## 2    38.5088    38.7450    38.6505    38.6033    37.8000    37.8000    37.8945
## 3    16.2059    16.3021    16.5425    16.4944    16.4464    16.4464    16.2540
## 4    26.4417    26.5360    26.6774    26.8188    26.4889    26.6303    26.8659
## 5    13.6253    13.7677    13.8152    13.8152    13.9101    13.9101    13.9576
## 6    16.2703    16.3649    16.4595    16.3649    16.3649    16.4122    16.4122
##   2025-02-13 2025-02-14 2025-02-17 2025-02-18 2025-02-19 2025-02-20 2025-02-21
## 1    32.1708    31.7861    32.2189    32.1708    31.7861    32.6517    32.8922
## 2    39.0758    38.9340    39.2175    39.1230    38.8395    39.2648    39.6900
## 3    16.6387    16.7349    16.6387    16.3502    16.3502    16.4944    16.5425
## 4    27.4787    27.7143    27.7615    27.5729    27.6672    27.8557    27.9500
## 5    14.1950    14.1475    14.1475    14.0525    14.0525    14.1000    14.1475
## 6    16.6014    16.7905    16.8378    16.8851    16.8378    16.9324    16.8851
##   2025-02-24 2025-02-25 2025-02-26 2025-02-27 2025-03-03 2025-03-04 2025-03-05
## 1    32.6998    33.6135    33.9020    33.7577    33.6616    33.2769    33.8058
## 2    39.7845    39.5955    39.7845    39.5010    41.6745    41.2965    41.1548
## 3    16.4944    16.7349    16.5906    15.7250    15.7731    15.8212    15.9174
## 4    27.9500    27.9971    28.2799    28.5627    28.9869    28.6570    28.7513
## 5    14.1475    14.2899    14.3374    14.2899    14.1950    14.2424    14.2424
## 6    16.8851    16.9324    16.8851    16.9797    16.9797    17.0743    17.0743
##   2025-03-06 2025-03-07 2025-03-10 2025-03-11 2025-03-12 2025-03-13 2025-03-14
## 1    33.4692    33.1807    33.4211    33.4692    33.1807    32.1228    32.5555
## 2    41.3438    41.5800    41.6273    41.7218    41.6745    41.6745    42.1470
## 3    16.0616    15.9174    15.9174    15.7250    15.7731    15.5327    15.7250
## 4    28.5627    28.2799    28.4213    28.4685    28.2799    28.1857    28.5156
## 5    14.2424    14.2424    14.2899    14.3848    14.4798    14.4798    14.4798
## 6    17.0270    16.9324    16.9797    16.8851    16.8378    16.8378    16.8851
##   2025-03-17 2025-03-18 2025-03-19 2025-03-20 2025-03-21 2025-03-24 2025-03-25
## 1    32.4113    32.3151    32.3632    32.4113    32.2189    31.8823    31.5457
## 2    41.8635    42.5250    42.5723    42.9975    42.6195    43.2338    43.3283
## 3    15.7250    15.8212    15.7250    15.7250    16.2540    15.7731    15.6769
## 4    28.4685    28.5156    28.6099    28.6099    28.3742    28.4685    28.5156
## 5    14.5273    14.1000    14.1950    14.3848    14.2899    14.2899    14.2424
## 6    16.9324    16.9797    17.0743    17.0270    16.8851    16.8378    16.9324
##   2025-03-26 2025-03-27 2025-03-28 2025-03-31 2025-04-01 2025-04-02 2025-04-07
## 1    31.7861    31.9785    31.5938    30.8244    31.1610    31.1610    28.6604
## 2    44.0370    44.5568    44.4150    44.1315    44.3678    44.6513    40.2098
## 3    15.6769    15.6769    15.4846    15.1960    15.3403    15.3884    13.8496
## 4    28.3742    28.3271    28.0914    27.5258    27.9029    27.9500    25.1691
## 5    14.2899    14.2424    14.0525    13.9576    14.1000    14.0051    12.6283
## 6    16.9324    16.8378    16.6959    16.5541    16.6486    16.6014    14.9459
##   2025-04-08 2025-04-09 2025-04-10 2025-04-11 2025-04-14 2025-04-15 2025-04-16
## 1    28.5162    27.4101    29.5741    28.8528    28.7566    28.7566    28.3719
## 2    41.4383    40.9185    42.6668    42.7613    42.8085    42.3833    41.6273
## 3    13.7053    13.0321    14.3304    14.2343    14.1862    14.3785    14.0900
## 4    25.2634    24.4150    26.5831    26.0175    25.6876    26.0647    25.6876
## 5    12.3434    12.1061    13.1505    13.1505    12.9131    13.1505    13.0556
## 6    15.0878    14.6622    15.7973    15.5608    15.4662    15.9392    15.9392
##   2025-04-17 2025-04-18 2025-04-21 2025-04-22 2025-04-23 2025-04-24 2025-04-25
## 1    28.2276    27.8429    27.6025    27.5063    28.1315    28.1315    28.2276
## 2    42.0525    42.0525    42.3833    41.8163    42.0053    42.0053    41.5328
## 3    14.1381    14.2343    14.0419    13.9938    14.1381    14.2343    14.3785
## 4    25.5933    25.8761    25.4048    25.4519    25.7819    25.9233    26.0175
## 5    13.1030    13.0556    13.0081    12.8657    12.9606    12.9606    13.0556
## 6    15.8446    15.7500    15.6554    15.4662    15.4662    15.4189    15.4189
##   2025-04-28 2025-04-29 2025-04-30 2025-05-02 2025-05-05 2025-05-06 2025-05-07
## 1    28.2276    28.3719    28.5162    28.8047    29.4298    29.3817    29.6703
## 2    42.1943    42.4305    42.5250    42.3833    43.0448    43.1393    43.1865
## 3    14.3304    14.4266    14.2824    14.5709    15.0037    15.2441    15.3403
## 4    26.3475    26.6303    26.3475    26.6774    26.8188    27.0073    27.0545
## 5    13.1030    13.2455    13.1505    13.8152    14.2899    14.2424    14.1475
## 6    15.6081    15.8446    15.8919    16.0338    16.4595    16.5541    16.6486
##   2025-05-08 2025-05-09 2025-05-12 2025-05-13 2025-05-14 2025-05-15 2025-05-16
## 1    29.3817    29.5741    30.0069    29.9107    29.1894    28.9489    28.9489
## 2    42.5723    43.0448    42.6195    42.7613    42.4305    41.8163    41.9580
## 3    15.1480    15.1960    15.1480    15.1960    15.1480    15.0999    15.1960
## 4    27.4315    27.6201    27.9971    27.4787    27.5729    27.4787    27.7615
## 5    14.1950    14.1950    14.3374    14.3848    14.5273    14.5273    14.4798
## 6    16.2703    16.3176    16.3176    16.2230    16.4122    16.2703    16.3176
##   2025-05-19 2025-05-20 2025-05-21 2025-05-22 2025-05-23 2025-05-26 2025-05-27
## 1    28.6604    28.9970    29.1413    28.8528    28.6123    28.7085    28.2276
## 2    41.7690    42.6195    42.3360    40.9658    40.5878    40.3515    39.6900
## 3    15.2441    15.2922    15.5327    15.3884    15.2922    15.0518    14.7632
## 4    27.7143    27.8557    28.0914    27.9971    28.7513    28.9869    29.0812
## 5    14.4798    14.5273    14.4798    14.5273    14.4798    14.4798    14.4323
## 6    16.2230    16.4595    16.5068    16.4122    16.3176    16.5541    16.3649
##   2025-05-28 2025-05-29 2025-06-02 2025-06-03 2025-06-04 2025-06-05 2025-06-06
## 1    27.9872    28.2276    26.6407    25.8232    25.9675    26.4484    26.4965
## 2    40.0208    39.5483    39.4065    39.5010    39.1230    39.3593    39.7845
## 3    14.5709    14.5709    14.1381    14.0900    14.2343    14.3785    14.5228
## 4    28.8455    29.1283    28.3271    28.4685    29.3169    28.7513    28.5156
## 5    14.4323    14.4798    14.3374    14.2899    14.3374    14.3374    14.2899
## 6    16.3176    16.4122    16.0811    15.8919    15.9865    16.1757    16.1284
##   2025-06-09 2025-06-10 2025-06-11 2025-06-12 2025-06-13 2025-06-16 2025-06-17
## 1    26.1598    26.1118    26.2560    26.0156    25.8713    25.7751    25.7271
## 2    39.8790    40.4460    40.7768    40.4460    41.0130    40.5405    40.4460
## 3    14.4747    14.4266    14.1381    14.1862    13.9457    13.9938    14.0419
## 4    28.3742    28.4685    28.3742    28.2799    28.1857    28.4213    28.4213
## 5    14.2424    14.1950    14.1950    14.1950    14.1475    14.1475    14.1000
## 6    16.1284    16.1284    16.1284    16.0338    15.9392    16.0811    16.0811
##   2025-06-18 2025-06-19 2025-06-20 2025-06-23 2025-06-24 2025-06-25 2025-06-26
## 1    25.4866    25.0538    24.5249    24.1402    24.7653    24.7172    24.8134
## 2    40.1153    39.5483    39.5483    39.5955    39.9735    39.9263    40.3043
## 3    13.9938    13.7534    13.5129    13.5129    13.8496    13.8976    14.0900
## 4    28.7041    28.1857    28.5627    27.9971    27.9500    27.9000    28.1500
## 5    14.1475    14.0051    13.9576    13.7677    14.1475    14.1000    14.1475
## 6    15.9865    15.8446    15.7973    15.7027    15.8919    15.9865    15.9392
##   2025-06-27 2025-06-30
## 1    25.1981    24.5249
## 2    40.8240    40.3043
## 3    14.1381    14.0900
## 4    28.1000    27.8500
## 5    14.1000    14.0525
## 6    15.9865    15.9392
data_filtered <- data_spread %>%
  tidyr::drop_na()
filtered_rows <- nrow(data_filtered)
filtered_cols <- ncol(data_filtered)

cat(filtered_rows, filtered_cols)
## 936 359
library(lubridate)
library(PerformanceAnalytics)
library(xts) # Add xts library explicitly if not already loaded

# Note: Ensure your 'data_filtered' tibble is available from Question 6

# -----------------------------------------------
# STEP 2: FIX - Convert to Time Series (xts)
# -----------------------------------------------

# 1. Prepare Data Matrix: Set 'id' as row names and transpose to get [Date x Stock] matrix
data_matrix <- data_filtered %>%
  column_to_rownames(var = "id") %>%
  t() %>%
  as.matrix() 

# ๐Ÿ’ก CRITICAL FIX: Ensure the matrix is numeric before creating xts
# This solves issues where the transpose function coerces prices to character.
data_matrix_numeric <- apply(data_matrix, 2, as.numeric)
rownames(data_matrix_numeric) <- rownames(data_matrix) # Preserve the date rownames

# 2. Extract and Convert Index (Dates)
date_index <- rownames(data_matrix_numeric) %>% # Use the corrected matrix's rownames
  ymd() %>%
  as.Date()

# 3. Create the xts Object using the base xts() function
stock_xts <- xts(
  x = data_matrix_numeric, # Use the confirmed numeric matrix
  order.by = date_index
)

# -----------------------------------------------
# STEP 3 & 4 (Return Calculation and Display)
# -----------------------------------------------

# This explicit call should now work with the correct numeric input data.
returns_xts <- (stock_xts / stats::lag(stock_xts, k = 1)) - 1

# -----------------------------------------------
# STEP 4: Delete the first row and show results
# -----------------------------------------------

# The first row is always NA.
# We delete the first row using standard xts indexing [row_start:row_end, col_start:col_end].
returns_xts_clean <- returns_xts[2:nrow(returns_xts), 1:5] # Selects row 2 to end, and col 1 to 5

cat("## Filtered Daily Discrete Returns (xts) \n")
## ## Filtered Daily Discrete Returns (xts)
# Show the first 5 rows and first 5 columns (stocks)
print(returns_xts_clean[1:5, 1:5])
##                    1101         1102         1103         1104         1108
## 2024-01-03 -0.014408653 -0.012149290 -0.007958236 -0.006733735 -0.003160781
## 2024-01-04  0.000000000  0.012298711  0.000000000 -0.013558772  0.003170803
## 2024-01-05  0.004384536 -0.001214929  0.002674026  0.010306896  0.000000000
## 2024-01-08 -0.002909225  0.004865628  0.002666895 -0.003399291  0.006328664
## 2024-01-09 -0.005841680 -0.007263102 -0.002659801 -0.013655209 -0.025155457
data_matrix <- data_filtered %>%
  column_to_rownames(var = "id") %>%
  t() %>%
  as.matrix() 

# 2. Extract and Convert Index (Dates)
date_index <- rownames(data_matrix) %>%
  ymd() %>%        
  as.Date()        

# 3. Create the daily xts Object
stock_xts_daily <- xts(
  x = data_matrix,  
  order.by = date_index 
)

# --- 3. Compute Monthly Returns ---

# A. Convert Daily Prices to Monthly Prices (uses the last day's closing price)
stock_xts_monthly_prices <- to.monthly(stock_xts_daily, indexAt = "lastof")[, -1] # Remove Open, High, Low columns left by to.monthly

# B. Calculate Discrete Monthly Returns
# FIX: Use stats::lag() to calculate returns correctly.
returns_xts_monthly <- (stock_xts_monthly_prices / stats::lag(stock_xts_monthly_prices, k = 1)) - 1

# C. Delete the first row (NA)
returns_xts_clean_monthly <- returns_xts_monthly[2:nrow(returns_xts_monthly), ]

# --- 4. Show Results ---

cat("## Filtered Monthly Discrete Returns (xts) \n")
## ## Filtered Monthly Discrete Returns (xts)
# Show the first 5 rows (months) and first 5 columns (stocks)
print(returns_xts_clean_monthly)
##            stock_xts_daily.High stock_xts_daily.Low stock_xts_daily.Close
## 2024-02-29         -0.020581479        -0.008404066           0.018870137
## 2024-03-31          0.028429706        -0.033901170           0.063972405
## 2024-04-30          0.050483119         0.011696930           0.112340020
## 2024-05-31          0.052630642         0.034685080          -0.032714871
## 2024-06-30         -0.024999577         0.013967648           0.048528298
## 2024-07-31          0.024525881         0.010637546           0.004913039
## 2024-08-31          0.030459602        -0.036417225          -0.073462520
## 2024-09-30          0.058759488         0.000000000           0.000000000
## 2024-10-31         -0.002094239         0.017445972          -0.021034527
## 2024-11-30         -0.001050427         0.000000000          -0.033059097
## 2024-12-31         -0.086134454        -0.037145522          -0.022221094
## 2025-01-31         -0.048275862        -0.044508481          -0.003497749
## 2025-02-28          0.016908213         0.015525102           0.063158130
## 2025-03-31          0.119953751        -0.033640700          -0.036302590
## 2025-04-30          0.002120888        -0.142399316          -0.042807112
## 2025-05-31         -0.032805316         0.118077670           0.105543220
## 2025-06-30         -0.050328228        -0.072610477          -0.043885156
raw_data <- read_delim(
  file = "/Users/Frezza/Downloads/tej_day_price_2024_20250630.txt",
  delim = "\t",
) %>%
  # Convert Date to Date object
  mutate(Date = ymd(Date)) %>%
  # Rename the Market Cap column for easier use
  rename(Market_Cap = `Market Cap.(NTD MN)`)
## Rows: 337347 Columns: 12
## โ”€โ”€ Column specification โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
## Delimiter: "\t"
## chr (3): CO_ID, TSE Sector, English Short Name
## dbl (9): Date, TSE ID, Open(NTD), High(NTD), Low(NTD), Close(NTD), Volume(10...
## 
## โ„น Use `spec()` to retrieve the full column specification for this data.
## โ„น Specify the column types or set `show_col_types = FALSE` to quiet this message.
# --- 2. Define Year-End Dates ---

# Last available date in 2024 (Find Max Date where year is 2024)
end_2024_date <- raw_data %>%
  filter(year(Date) == 2024) %>%
  pull(Date) %>%
  max()

# Last available date in 2025 (Last date in file, which is 2025-06-30)
end_2025_date <- raw_data %>%
  filter(year(Date) == 2025) %>%
  pull(Date) %>%
  max()

# --- 3. Filter and Find Top 20 for Each Period ---

# A. Top 20 at 2024 Year-End
top_20_2024 <- raw_data %>%
  filter(Date == end_2024_date) %>%
  arrange(desc(Market_Cap)) %>%
  slice(1:20) %>%
  select(Date, `TSE ID`, `English Short Name`, Market_Cap) %>%
  mutate(Rank = row_number()) %>%
  rename(`2024 Rank` = Rank, `2024 Market Cap (NTD MN)` = Market_Cap)

# B. Top 20 at 2025 Year-End (June 30)
top_20_2025 <- raw_data %>%
  filter(Date == end_2025_date) %>%
  arrange(desc(Market_Cap)) %>%
  slice(1:20) %>%
  select(Date, `TSE ID`, `English Short Name`, Market_Cap) %>%
  mutate(Rank = row_number()) %>%
  rename(`2025 Rank` = Rank, `2025 Market Cap (NTD MN)` = Market_Cap)

# --- 4. Merge Results for Combined View ---

# Combine the two lists to show ranking changes
top_20_combined <- full_join(
  top_20_2024 %>% select(`TSE ID`, `English Short Name`, `2024 Rank`, `2024 Market Cap (NTD MN)`),
  top_20_2025 %>% select(`TSE ID`, `English Short Name`, `2025 Rank`, `2025 Market Cap (NTD MN)`),
  by = c("TSE ID", "English Short Name")
) %>%
  # Arrange by the most recent rank (2025)
  arrange(`2025 Rank`) %>%
  # Fill NA ranks with the maximum rank to keep unranked firms at the bottom
  mutate(`2024 Rank` = replace_na(`2024 Rank`, max(`2024 Rank`, na.rm = TRUE) + 1))

print(top_20_combined)
## # A tibble: 20 ร— 6
##    `TSE ID` `English Short Name` `2024 Rank` 2024 Market Cap (NTD โ€ฆยน `2025 Rank`
##       <dbl> <chr>                      <int>                   <dbl>       <int>
##  1     2330 TSMC                           1                27877688           1
##  2     2317 Hon Hai                        2                 2556073           2
##  3     2454 MediaTek                       3                 2266389           3
##  4     2881 Fubon Financial                4                 1234015           4
##  5     2308 DELTA                          5                 1118242           5
##  6     2382 QCI                            6                 1108574           6
##  7     2412 CHT                            8                  958045           7
##  8     2882 CATHAY FHC                     7                 1001907           8
##  9     2891 CTBC Holding                   9                  767154           9
## 10     3711 ASEH                          10                  715219          10
## 11     2886 Mega FHC                      11                  574052          11
## 12     2303 UMC                           12                  540739          12
## 13     2884 E.S.F.H                       19                  431087          13
## 14     2357 Asustek                       16                  457540          14
## 15     6669 Wiwynn                        14                  486903          15
## 16     1216 Uni-President                 15                  459675          16
## 17     2885 Yuanta Group                  17                  440057          17
## 18     2603 EMC                           13                  487135          18
## 19     3045 TWM                           20                  422590          19
## 20     2345 Accton                        18                  433744          20
## # โ„น abbreviated name: ยนโ€‹`2024 Market Cap (NTD MN)`
## # โ„น 1 more variable: `2025 Market Cap (NTD MN)` <dbl>