2016-03-11 161 views
-3

我有一个数据框,它有4000多列和3000行。列是公司和行有每日股票收盘价格。这些行具有基于月份日期的每日观察数据。现在,我想要删除每个月的最后一个日期之间的行,即我想根据月份的最后一天的数据形成我的数据框。每个月的最后日期应根据我的数据框中的日期列可用。 我的问题对他人的主要挑战和差异是上个月的日期应该根据我的数据框中提供的日期。它的财务数据和非交易日,没有。的交易日不同于其他类型的行业 我举例说明了我的数据框的一部分。根据R中的日期删除数据帧的行

Date  A B 
30/12/1999 1 3 
04/01/2000 1 3 
05/01/2000 1 3 
06/01/2000 1 3 
07/01/2000 1 3 
10/01/2000 1 3 
11/01/2000 1 3 
12/01/2000 1 3 
13/01/2000 1 3 
14/01/2000 1 3 
17/01/2000 1 3 
18/01/2000 1 3 
19/01/2000 1 3 
20/01/2000 1 3 
21/01/2000 1 3 
24/01/2000 1 3 
25/01/2000 1 3 
26/01/2000 1 3 
27/01/2000 1 3 
28/01/2000 1 3 
31/01/2000 1 3 
01/02/2000 1 3 
02/02/2000 1 3 
03/02/2000 1 3 
04/02/2000 1 3 
07/02/2000 1 3 
08/02/2000 1 3 
09/02/2000 1 3 
10/02/2000 1 3 
11/02/2000 1 3 
14/02/2000 1 3 
15/02/2000 1 3 
16/02/2000 1 3 
17/02/2000 1 3 
18/02/2000 1 3 
21/02/2000 1 3 
22/02/2000 1 3 
23/02/2000 1 3 
24/02/2000 1 3 
25/02/2000 1 3 
28/02/2000 1 3 
29/02/2000 1 3 

所需的输出

Date  A B 
30/12/1999 1 3 
31/01/2000 1 3 
29/02/2000 1 3 

我会很感激你在这方面的帮助。

+4

我们必须假设您在提问前彻底搜索过。请详细说明为什么[**这些答案**](http://stackoverflow.com/search?tab=votes&q=%20%5br%5d%20last%20day%20month)没有帮助。 “这个问题没有显示任何研究工作”,但只是plz发送codez。 – Henrik

+0

@Henrik我的问题对他人的主要挑战和差异是上个月的日期应该根据我的数据框中提供的日期。它的财务数据和非交易日,没有。的交易日与其他类型的行业不同。 – Aquarius

+0

@Aquarius看看'lubridate'和'zoo'包装。 – Sotos

回答

3

使用lubridatedplyr,第一解析Date

library(lubridate) 
library(dplyr) 
df$Date <- dmy(df$Date) 

现在我们可以建立一个dplyr链进行过滤:

df %>% group_by(month = month(Date), year = year(Date)) %>% filter(Date == max(Date)) 

我们group_bymonthyear列我们添加,然后filter下来仅限于每个组的max日期。它返回

Source: local data frame [3 x 5] 
Groups: month, year [3] 

     Date  A  B month year 
     (time) (int) (int) (dbl) (dbl) 
1 1999-12-30  1  3 12 1999 
2 2000-01-31  1  3  1 2000 
3 2000-02-29  1  3  2 2000 

你可以,当然,做这一切的基础R如果你喜欢。

编辑: H/T @Jaap推荐使用group_by添加列而不是单独的mutate。您也可以使用slice(which.max(Date))而不是filter的术语;如果这是一个问题,它可能会提示更快。

+0

感谢@akrun,这是我的错,因为我没有提供类似的日期格式,但是当我编写csv文件时它自身发生变化,所以谢谢你完美的作品。 – Aquarius

2

我们也可以使用data.table

library(data.table) 
library(lubridate) 
setDT(df1)[, c('month', 'year', 'Date') :={tmp <- dmy(Date) 
    list(month= month(tmp), year= year(tmp), Date= tmp)} 
    ][, .SD[ which.max(Date)] ,.(month, year)] 
# month year  Date A B 
#1: 12 1999 1999-12-30 1 3 
#2:  1 2000 2000-01-31 1 3 
#3:  2 2000 2000-02-29 1 3 
+0

实际上我的数据帧在r中的日期格式为'1999-12-30',因此,我收到错误'警告消息: 所有格式都无法解析。找不到格式。 ' – Aquarius

+1

@Aquarius如果是这种情况而不是'dmy',那么应该在代码 – akrun

+1

中使用'ymd(Date)',谢谢!你有一个想法是什么问题的孩子,当涉及到R代码。答案是否定的。 1对于像我这样的人来说很容易理解。虽然我已经根据你的帮助做出了改变。 – Aquarius

2

这里的另一种可能性:

month_year <- as.numeric(as.factor(sub("^[0-9]*/","",df1$Date))) 
df1[!!c(diff(month_year),1),] 
#   Date A B 
#1 30/12/1999 1 3 
#21 31/01/2000 1 3 
#42 29/02/2000 1 3 

该解决方案不会改变日期的格式在原来的数据帧。但是,假定数据按照OP中显示的数据按时间顺序排序。

数据

df1 <- structure(list(Date = structure(c(41L, 4L, 6L, 7L, 8L, 12L, 14L, 
16L, 17L, 18L, 22L, 24L, 26L, 27L, 28L, 32L, 34L, 36L, 37L, 38L, 
42L, 1L, 2L, 3L, 5L, 9L, 10L, 11L, 13L, 15L, 19L, 20L, 21L, 23L, 
25L, 29L, 30L, 31L, 33L, 35L, 39L, 40L), .Label = c("01/02/2000", 
"02/02/2000", "03/02/2000", "04/01/2000", "04/02/2000", "05/01/2000", 
"06/01/2000", "07/01/2000", "07/02/2000", "08/02/2000", "09/02/2000", 
"10/01/2000", "10/02/2000", "11/01/2000", "11/02/2000", "12/01/2000", 
"13/01/2000", "14/01/2000", "14/02/2000", "15/02/2000", "16/02/2000", 
"17/01/2000", "17/02/2000", "18/01/2000", "18/02/2000", "19/01/2000", 
"20/01/2000", "21/01/2000", "21/02/2000", "22/02/2000", "23/02/2000", 
"24/01/2000", "24/02/2000", "25/01/2000", "25/02/2000", "26/01/2000", 
"27/01/2000", "28/01/2000", "28/02/2000", "29/02/2000", "30/12/1999", 
"31/01/2000"), class = "factor"), A = 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, 1L, 1L), B = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L 
)), .Names = c("Date", "A", "B"), class = "data.frame", row.names = c(NA, 
-42L)) 
1

我想创建一个包含一个月中日期的最后一个矢量数据,像这样:

library(dplyr) 
df.dates = seq(as.Date("1999-01-01"),as.Date(Sys.Date()),by="months")-1 
df.dates = as.data.frame(df.dates) 
names(df.dates) = "Date" 
df.joined = inner_join(df.dates, df) 

这是假设你有你的数据在数据帧与日期列名为“日期”

*重新阅读问题,如果最后一个交易日不是星期一的最后一天日。@alistaire有一个更好的解决方案,使用max(日期)