首页 \ 问答 \ R指定一个月中的某个星期(R specify what week in a month)

R指定一个月中的某个星期(R specify what week in a month)

我有一些股票回报每日数据需要变成每周格式。 由于您仅在周一至周五才了解股票交易,因此我需要累计每天的回报才能获得累积的每周回报。

我曾考虑过使用lubridate的星期功能,但lubridate如何知道本周开始的时间? 我如何使用平日功能来识别星期,即“星期一”到“星期五”是一周?

例如,我曾想过编写一个循环:如果数据中包含“星期一”到“星期五”,那么我将在这一周内调用它。 但是第二周,我会用什么让R知道我们正在进入第二周? 那么当我们到达年底并且有52周的时候,如何重新设置周计数,以便我们进入下一年?

这里是dput:

dat = structure(list(date = structure(c(4019, 4022, 4023, 4024, 4025, 
4026, 4029, 4030, 4031, 4032, 4033, 4036, 4037, 4038, 4039, 4040, 
4043, 4044, 4045, 4046, 4047, 4050, 4051, 4052, 4053, 4054, 4057, 
4058, 4059, 4060, 4061, 4065, 4066, 4067, 4068, 4071, 4072, 4073, 
4074, 4075), class = "Date"), weekday = c("Friday", "Monday", 
"Tuesday", "Wednesday", "Thursday", "Friday", "Monday", "Tuesday", 
"Wednesday", "Thursday", "Friday", "Monday", "Tuesday", "Wednesday", 
"Thursday", "Friday", "Monday", "Tuesday", "Wednesday", "Thursday", 
"Friday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Tuesday", 
"Wednesday", "Thursday", "Friday", "Monday", "Tuesday", "Wednesday", 
"Thursday", "Friday"), COMP = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), week = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 
4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 
9, 9), RET = c(-0.005435, 0.040984, -0.015748, -0.021333, 0.002725, 
0.01087, 0.024194, -0.002625, 0.013158, 0.033766, 0, -0.007538, 
-0.005063, 0, -0.002545, 0.015306, 0.017588, -0.007407, 0.024876, 
-0.009709, 0, -0.029412, 0.010101, 0.0075, -0.004963, 0.027431, 
-0.002427, 0.007299, -0.009662, -0.004878, 0.014706, -0.004831, 
0.004854, -0.009662, -0.021951, -0.014963, 0.005063, -0.005038, 
0.010127, 0)), .Names = c("date", "weekday", "COMP", "week", 
"RET"), row.names = c(NA, -40L), class = c("data.table", "data.frame"
))

library(data.table)
setDT(dat)

以下是公司1每日收益从1981-01-02到1981-02-27两个月的数据。 让我们暂时计算回报,并首先关注时间。

周列由周()函数生成。 正如你所看到的那一周不是我想要的,它从星期三开始到星期三结束。

星期几是由weekdays()函数生成的。

我想在第一周做出1981-01-02(因为我们在这里只有星期五),1981-01-05到1981-01-09为第二周,反之亦然。


I have some stock return daily data need to turn into weekly format. As you know stock trade only on Monday to Friday, I need to add up each days return to get cumulative weekly return.

I have thought about using lubridate's week function but how does lubridate knows when is the start of the week? How do I make lubridate to recognize the week using weekday function i.e. "Monday" to "Friday" is one week?

I have thought about writing a loop for example: If "Monday" to "Friday" is in the data, then I will call this one week. But for the second week, what shall I use for R to know we are entering second week? Then when we reach year end and we have 52 weeks, how to reset the week count so we are entering the next year?

Here is the dput:

dat = structure(list(date = structure(c(4019, 4022, 4023, 4024, 4025, 
4026, 4029, 4030, 4031, 4032, 4033, 4036, 4037, 4038, 4039, 4040, 
4043, 4044, 4045, 4046, 4047, 4050, 4051, 4052, 4053, 4054, 4057, 
4058, 4059, 4060, 4061, 4065, 4066, 4067, 4068, 4071, 4072, 4073, 
4074, 4075), class = "Date"), weekday = c("Friday", "Monday", 
"Tuesday", "Wednesday", "Thursday", "Friday", "Monday", "Tuesday", 
"Wednesday", "Thursday", "Friday", "Monday", "Tuesday", "Wednesday", 
"Thursday", "Friday", "Monday", "Tuesday", "Wednesday", "Thursday", 
"Friday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Tuesday", 
"Wednesday", "Thursday", "Friday", "Monday", "Tuesday", "Wednesday", 
"Thursday", "Friday"), COMP = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), week = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 
4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 
9, 9), RET = c(-0.005435, 0.040984, -0.015748, -0.021333, 0.002725, 
0.01087, 0.024194, -0.002625, 0.013158, 0.033766, 0, -0.007538, 
-0.005063, 0, -0.002545, 0.015306, 0.017588, -0.007407, 0.024876, 
-0.009709, 0, -0.029412, 0.010101, 0.0075, -0.004963, 0.027431, 
-0.002427, 0.007299, -0.009662, -0.004878, 0.014706, -0.004831, 
0.004854, -0.009662, -0.021951, -0.014963, 0.005063, -0.005038, 
0.010127, 0)), .Names = c("date", "weekday", "COMP", "week", 
"RET"), row.names = c(NA, -40L), class = c("data.table", "data.frame"
))

library(data.table)
setDT(dat)

Here are two month worth of data from 1981-01-02 to 1981-02-27 of company 1's daily return. Lets ignore to calculate the return at the moment and focus on the time first.

week column is generated by weeks() function. As you can see week is not what I wanted, it starts from wednesday and end in wednesday.

weekday is generated by weekdays() function.

I want to make e.g. 1981-01-02 as week 1 (since we have friday here only), 1981-01-05 to 1981-01-09 as week 2 vice versa.


原文:https://stackoverflow.com/questions/50706495
更新时间:2021-10-09 11:10

最满意答案

    while(true){
        Socket sk = sok.accept();
        in = sk.getInputStream();
        out = sk.getOutputStream();
    Dr    byte[] buffer = new byte[1024];
        in.read(buffer);
        String recievedData = new String(buffer);
        System.out.println(recievedData);
        out.write(recievedData.getBytes());

那个循环是错误的。 收到一条消息后,服务器将丢弃sk并等待新客户端。

你最好试试;

        Socket sk = sok.accept();
        in = sk.getInputStream();
        out = sk.getOutputStream();
        byte[] buffer = new byte[1024];
    while(true){
        int nread = in.read(buffer);
        String recievedData = new String(buffer, 0, nread);
        System.out.println(recievedData);
        out.write(recievedData.getBytes());

    while(true){
        Socket sk = sok.accept();
        in = sk.getInputStream();
        out = sk.getOutputStream();
    Dr    byte[] buffer = new byte[1024];
        in.read(buffer);
        String recievedData = new String(buffer);
        System.out.println(recievedData);
        out.write(recievedData.getBytes());

That loop is wrong. After one received message the server drops sk and will go waiting for a new client.

You better try;

        Socket sk = sok.accept();
        in = sk.getInputStream();
        out = sk.getOutputStream();
        byte[] buffer = new byte[1024];
    while(true){
        int nread = in.read(buffer);
        String recievedData = new String(buffer, 0, nread);
        System.out.println(recievedData);
        out.write(recievedData.getBytes());

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)