How do I paste data from the first day of the month for the whole month?

Maurits

I have a data frame containing weights for stocks in a portfolio, it is around 1000 stocks and 4000 days of data. I want to apply the weights of the first day of each month to all days of that month. However, I still want to retain the structure of daily data.

My data is similar to this:

data <- as.data.frame(matrix(1:4000, nrow = 200, ncol = 20))
rownames(data) <- seq(as.Date("2018/01/01"), as.Date("2018/07/19"), 1)

So I want to have the values of the first of January copied to all days of January, values of the first day of February copied to all days in Februari etc. I have no clue how to handle this.

Any tips?

nurandi

You might want to use grouped dplyr::mutate_at:

library(dplyr)

data %>%
  rownames_to_column("Date") %>%
  mutate(Month = format(as.Date(Date), "%Y-%m")) %>%
  group_by(Month) %>%
  mutate_at(vars(starts_with("V")), .funs = list(weight = ~first(.))) %>%
  column_to_rownames("Date") %>%
  select(Month, starts_with("V"))

Output

#              Month V1  V2  V3  V4  V5   V6   V7   V8   V9  V10  V11  V12  V13  V14  V15  V16  V17  V18  V19  V20 V1_weight
# 2018-01-01 2018-01  1 201 401 601 801 1001 1201 1401 1601 1801 2001 2201 2401 2601 2801 3001 3201 3401 3601 3801         1
# 2018-01-02 2018-01  2 202 402 602 802 1002 1202 1402 1602 1802 2002 2202 2402 2602 2802 3002 3202 3402 3602 3802         1
# 2018-01-03 2018-01  3 203 403 603 803 1003 1203 1403 1603 1803 2003 2203 2403 2603 2803 3003 3203 3403 3603 3803         1
# 2018-01-04 2018-01  4 204 404 604 804 1004 1204 1404 1604 1804 2004 2204 2404 2604 2804 3004 3204 3404 3604 3804         1
# ...
# 2018-01-21 2018-01 21 221 421 621 821 1021 1221 1421 1621 1821 2021 2221 2421 2621 2821 3021 3221 3421 3621 3821         1
# 2018-01-22 2018-01 22 222 422 622 822 1022 1222 1422 1622 1822 2022 2222 2422 2622 2822 3022 3222 3422 3622 3822         1
# 2018-01-23 2018-01 23 223 423 623 823 1023 1223 1423 1623 1823 2023 2223 2423 2623 2823 3023 3223 3423 3623 3823         1
# 2018-01-24 2018-01 24 224 424 624 824 1024 1224 1424 1624 1824 2024 2224 2424 2624 2824 3024 3224 3424 3624 3824         1
#            V2_weight V3_weight V4_weight V5_weight V6_weight V7_weight V8_weight V9_weight V10_weight V11_weight V12_weight
# 2018-01-01       201       401       601       801      1001      1201      1401      1601       1801       2001       2201
# 2018-01-02       201       401       601       801      1001      1201      1401      1601       1801       2001       2201
# 2018-01-03       201       401       601       801      1001      1201      1401      1601       1801       2001       2201
# 2018-01-04       201       401       601       801      1001      1201      1401      1601       1801       2001       2201
# ...
# 2018-01-21       201       401       601       801      1001      1201      1401      1601       1801       2001       2201
# 2018-01-22       201       401       601       801      1001      1201      1401      1601       1801       2001       2201
# 2018-01-23       201       401       601       801      1001      1201      1401      1601       1801       2001       2201
# 2018-01-24       201       401       601       801      1001      1201      1401      1601       1801       2001       2201
#            V13_weight V14_weight V15_weight V16_weight V17_weight V18_weight V19_weight V20_weight
# 2018-01-01       2401       2601       2801       3001       3201       3401       3601       3801
# 2018-01-02       2401       2601       2801       3001       3201       3401       3601       3801
# 2018-01-03       2401       2601       2801       3001       3201       3401       3601       3801
# 2018-01-04       2401       2601       2801       3001       3201       3401       3601       3801
# ...
# 2018-01-20       2401       2601       2801       3001       3201       3401       3601       3801
# 2018-01-21       2401       2601       2801       3001       3201       3401       3601       3801
# 2018-01-22       2401       2601       2801       3001       3201       3401       3601       3801
# 2018-01-23       2401       2601       2801       3001       3201       3401       3601       3801
# 2018-01-24       2401       2601       2801       3001       3201       3401       3601       3801

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I find the first and last day of next month?

How to get data from first day of the month until yesterday

How do defining the first and last day of the month

How do I get the day of the month in javascript?

How do I get the last day of a month?

How do I check day for cross month

Get data only from the last day of month but calculate average of a column from whole month

How do i get first date and Last date from month and year |Excel |Used Spinner for Month and Year|

How do I Change the view from month to day view if i click on a specific day

How do i separate data from same month of different years?

How do I select between 1st day of the current month and the 1st day of the following month (from current month) in postgres

How do i get the first and last day value per month in pandas?

How do I combine multiple tables? (First has data from this month, second has all other previous data)

How do i get the whole last month in R?

How do i get the dates for the whole month in a dropdown? C#

SQL: how do you fetch a first day of the month?

how to allow user to input data only in first 7 day of month

How can I get the first day of the next month in Python?

How can I select the first day of a month in SQL?

Spring Data JPA - How can I fetch data from a Date column using only month and day?

How to get data of every first day of month for all month in my sql?

fullcalendar: how can i get the first and last day of the relevant viewed month (and not the days of of the prev \ next month)

SQL Finding the first day of the month of a data set

first day != "first day of this month"

How do I extract the Hour/Minute from a datetime object and get rid of the Year/Month/Day/Second section?

How do I get the average month and day from a list of date times?

How do i get the date from week of month and day of week with Carbon?

How do I specify from a date to the last occurence of a day in a specific month, using iCal?

Get the first and the last day of a month from the df