data <- read.table("MidtermDataTEJ.txt", header=TRUE, sep="\t", stringsAsFactors=FALSE)
data$Date <- as.Date(as.character(data$Date), format="%Y%m%d")
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
highest_prices <- data %>%
group_by(CO_ID, CoName) %>%
summarize(
Highest_Price = max(Close),
Date_of_Highest_Price = Date[which.max(Close)]
) %>%
arrange(desc(Highest_Price))
## `summarise()` has grouped output by 'CO_ID'. You can override using the
## `.groups` argument.
print(highest_prices)
## # A tibble: 3 × 4
## # Groups: CO_ID [3]
## CO_ID CoName Highest_Price Date_of_Highest_Price
## <dbl> <chr> <dbl> <date>
## 1 50 "Yuanta Taiwan Top50" 137. 2022-01-17
## 2 52 "FB Technology" 124. 2023-12-28
## 3 56 "PTD " 34.1 2023-12-27
overall_highest <- highest_prices %>%
slice(which.max(Highest_Price))
print("Overall highest price:")
## [1] "Overall highest price:"
print(overall_highest)
## # A tibble: 3 × 4
## # Groups: CO_ID [3]
## CO_ID CoName Highest_Price Date_of_Highest_Price
## <dbl> <chr> <dbl> <date>
## 1 50 "Yuanta Taiwan Top50" 137. 2022-01-17
## 2 52 "FB Technology" 124. 2023-12-28
## 3 56 "PTD " 34.1 2023-12-27