I have a tibble that contains monthly observations:
library(zoo)
library(dplyr)
mfs.dataframe <- structure(
list(
Date = structure(
c(
17197,
17225,
17256,
17286,
17317,
17347,
17378,
17409,
17439,
17470
),
class = "Date"
),
m2 = c(
9586.1485960711,
10133.2515139652,
10692.0071143624,
10442.8613115585,
10352.354832707,
10763.0662099058,
10869.735010259,
11174.5131264841,
11678.0194789741,
11548.8304023101
),
private_sector_claims = c(
5836.3121609,
5811.50043440506,
5841.654446964,
5801.247438522,
5981.69435068,
5934.79495271,
5936.83241575,
5967.327903756,
6160.67429487,
6068.69840886
),
business_ent_claims = c(
2166.00848066912,
2071.38826678,
2102.46072741634,
2065.760912972,
2179.23061007,
2100.98313658,
1989.63394849,
2051.25948937,
1967.7576887,
1999.08408221
)
),
row.names = c(NA,-10L),
class = c("tbl_df", "tbl", "data.frame")
)
I want to express in quarters, however since these are stocks, I have to take positions at the end of each quarter. I have this code:
mfs.dataframe <- mfs_df |> dplyr::mutate(
qdate = as.yearqtr(Date))
then code above creates a column with quarterly dates - qdate (i.e. 2017 Q1) however when I group using the column qdate to take the last observation of the group the code returns the last observation of the whole tibble
here is the code that I used:
mfsQtrly <- mfs_df |>
group_by(qdate) |>
summarise(qdate = last(qdate),
m2 = last(m2),
private_sector_claims = last(private_sector_claims),
total_deposits = last(total_deposits))
this only results in a tibble with 4 vars and 1 observation, it ignores the groups