2016-03-11 185 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(日期)