2010-08-06 55 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)