2010-08-06 56 views
4

我有選擇這樣- [R檢查對行的數據幀

> chData 
myIdx strike_price  date  exdate cp_flag strike_price return 
1 8355342  605000 1996-04-02 1996-05-18  P  605000 0.002340 
2 8355433  605000 1996-04-02 1996-05-18  C  605000 0.002340 
3 8356541  605000 1996-04-09 1996-05-18  P  605000 -0.003182 
4 8356629  605000 1996-04-09 1996-05-18  C  605000 -0.003182 
5 8358033  605000 1996-04-16 1996-05-18  P  605000 0.003907 
6 8358119  605000 1996-04-16 1996-05-18  C  605000 0.003907 
7 8359391  605000 1996-04-23 1996-05-18  P  605000 0.005695 

數據幀保持信息,其中cp_flag意味着某個選項可以是一個呼叫或一個看跌期權。什麼方法可以確保每個日期都有一個調用和一個put,並刪除不存在的行?我可以用for循環來做,但有沒有更聰明的方法?

回答

10

獲取具有P公司和那些有C'S,並使用相交發現有兩個日期的日期。

keep_dates <- with(x, intersect(date[cp_flag=='P'], date[cp_flag=='C'])) 
# "1996-04-02" "1996-04-09" "1996-04-16" 

只保留在keep_dates中出現日期的行。

x[ x$date %in% keep_dates, ] 
# myIdx strike_price  date  exdate cp_flag strike_price.1 
# 8355342  605000 1996-04-02 1996-05-18  P   605000 
# 8355433  605000 1996-04-02 1996-05-18  C   605000 
# 8356541  605000 1996-04-09 1996-05-18  P   605000 
# 8356629  605000 1996-04-09 1996-05-18  C   605000 
# 8358033  605000 1996-04-16 1996-05-18  P   605000 
# 8358119  605000 1996-04-16 1996-05-18  C   605000 
+0

優雅!我很喜歡這個。 – Vince 2010-08-06 05:48:24

0

下面是使用splitlapply一個辦法:

> tmp <- lapply(split(d, list(d$date)), function(x) if(all(c('P', 'C') %in% x[, 5])) x) 
> do.call(rbind, tmp) 
      myIdx strike_price  date  exdate cp_flag strike_price return 
1996-05-18.1 8355342  605000 1996-04-02 1996-05-18  P  605000 0.002340 
1996-05-18.2 8355433  605000 1996-04-02 1996-05-18  C  605000 0.002340 
1996-05-18.3 8356541  605000 1996-04-09 1996-05-18  P  605000 -0.003182 
1996-05-18.4 8356629  605000 1996-04-09 1996-05-18  C  605000 -0.003182 
1996-05-18.5 8358033  605000 1996-04-16 1996-05-18  P  605000 0.003907 
1996-05-18.6 8358119  605000 1996-04-16 1996-05-18  C  605000 0.003907 
1996-05-18.7 8359391  605000 1996-04-23 1996-05-18  P  605000 0.005695 

編輯:這是我的最終答案隱含的完整版本。我傾向於用基本功能而不是plyr或重塑......但這些答案看起來也不錯。

+0

我一定在服用瘋狂的藥丸......'lapply' +'split'最好只用'tapply'完成。但是,這個解決方案似乎*更清潔。 – Vince 2010-08-06 07:04:16

1

使用plyr包:

> ddply(chData, "date", function(x) if(all(c("P","C") %in% x$cp_flag)) x) 
    myIdx strike_price  date  exdate cp_flag strike_price.1 return 
1 8355342  605000 1996-04-02 1996-05-18  P   605000 0.002340 
2 8355433  605000 1996-04-02 1996-05-18  C   605000 0.002340 
3 8356541  605000 1996-04-09 1996-05-18  P   605000 -0.003182 
4 8356629  605000 1996-04-09 1996-05-18  C   605000 -0.003182 
5 8358033  605000 1996-04-16 1996-05-18  P   605000 0.003907 
6 8358119  605000 1996-04-16 1996-05-18  C   605000 0.003907 
+0

這種語言讓我越來越核心的隱祕和不直觀,我讀了更多。什麼是ddply plyr? – Karl 2010-08-06 04:27:00

+0

@Karl,這是一個包,而不是「核心」語言。 – Vince 2010-08-06 04:27:43

+0

它只是看起來很神祕,因爲那裏的功能。 「plyr」及其功能真的很棒。 – JoFrhwld 2010-08-06 05:01:12

1

這是reshape方法。

library(reshape) 
#Add a dummy value 
df$value <- 1 
check <- cast(df, myIdx + strike_price + date + exdate + strike_price + return ~ cp_flag) 

#take stock of what just happened 
summary(check) 

#use only complete cases. If you have NAs elsewhere, this will knock out those obs too 
check <- check[complete.cases(check),] 

#back to original form 
df.clean <- melt(check, id = 1:6)