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
最满意答案
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());
相关问答
更多-
Android SDK,从客户端到服务器的输出流不起作用(Android SDK, Output Stream from client to server doesn't work)[2023-03-22]
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.pri ... -
您将更好地使用DataInputStream和DataOutputStream在服务器和客户端之间进行通信。 在您的服务器类中实现Runnable ,并将run()方法添加到其中。 在while (true)循环中,从客户端接收数据并将其输出,如下所示: public void run() { while (true) { try { String line = dis.readUTF(); System.out.println(lin ...
-
如果您检查BluetoothChat应用程序的代码,它具有唯一的UUID,用于侦听接受传入连接和进行传出连接。 这个UUID对于传入/传出连接都是相同的。 MY_UUID = UUID.fromString(“fa87c0d0-afac-11de-8a39-0800200c9a66”); If you check the code for BluetoothChat application, its having a unique UUID to listen for accepting incoming ...
-
你描述的场景听起来对于C2DM来说非常理想。 如果你建立自己的套接字设置,你将耗尽电池和CPU资源来完成手机已经在做的事情 - 维护一个开放的套接字来接收消息。 C2DM的设置相对简单,根据我的经验,它非常可靠。 这是我见过的最好的教程: http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html 但是你也需要阅读官方文档: http://code.google.com/android/c2dm/ 您的服务器端代码将需要 ...
-
最后一行是否有回复? 如果不是readLine()将挂起,直到发送返回。 此外,如果您使用的是HTTP 1.1,则连接可能会保持打开状态并等待响应。 确保Content-Length标头也正确无误。 I found the problem. The reason it was hanging is because readLine() was waiting for the '\n' character to be recieved so i changed the server to send '\n' ...
-
好的,我喜欢的是这样的。 首先,我使用文件的对象,如下所示: class FileTransfer { public byte[] content; public string name; public FileTransfer(string name, byte[] content) { this.name= name; this.content= content; } } 然后我在Asp.Net上有一个Web服务,看起来像: ...
-
您的代码的问题不是它的通信协议的“套接字”。 在服务器有机会写出“hi”之前,您正在有效地关闭套接字。 要调试它,您希望降低程序的复杂性。 在您的计划中,有许多事情没有任何意义或问题。 所以,关于套接字的一点背景知识。 有两种类型的套接字。 “ServerSocket”和“Socket”ServerSocket有点像秘书。 它唯一的工作是听取电话然后传递它们。 这就是“接受”的作用。 在任何客户端连接之前,accept()将阻塞,直到它收到连接。 客户端连接后,accept返回表示连接的Socket。 常规 ...
-
使用J2SE,我的建议是从java.nio.channels.SocketChannel初始化Socket,并在合理的超时到期后中断被阻塞的线程。 我不确定你要修复哪一方,但看起来像J2ME你唯一的选择是设置套接字超时 。 编辑 实际上,既然您已经发布了客户端代码,我就会发现问题所在。 如果由于某种原因从while循环抛出异常,则输出流不会关闭。 这是我提出的修复方案: ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStr ...
-
尝试使用Asynctask包装requestServer() 。 在doInBackground()中执行此方法 Try wrapping requestServer() with an Asynctask. Execute this method in doInBackground()
-
从java服务器到Android客户端直播视频的最佳方式?(Best way to live stream video from java server to android client? [duplicate])[2021-12-06]
我终于成功通过 Java服务器上的RTP流式传输我的PC屏幕了 码: public static void main(String[] args) throws Exception { boolean found = new NativeDiscovery().discover(); System.out.println(found); System.out.println(LibVlc.INSTANCE.libvlc_get_version()); String med ...