二元旗帜有两个条件r

时间:2014-12-16 15:13:57

标签: r

HY! 我的样本数据是这样的:

Date       Quantity Price 
2010/01/01    1        4.00
2010/01/02    5        3.00
2010/01/03    3        3.00
2010/01/04    4        4.00
2010/01/05    1        2.50
2010/01/06    1        2.50
2010/01/07    1        2.50
2010/01/08    1        2.50

我希望在价格降低大于或等于25%并且在小于或等于3天期间填充二进制(1和0):否则为0。 有没有比使用条件IF更好的方法?

最终结果如下:

Date       Quantity   Price  Flag
2010/01/01    1        4.00   0
2010/01/02    5        3.00   1
2010/01/03    3        3.00   1
2010/01/04    4        4.00   0
2010/01/05    1        2.50   0
2010/01/06    1        2.50   0
2010/01/07    1        2.50   0
2010/01/08    1        2.50   0

谢谢! PS:具有一个产品http://speedy.sh/fbMYN/product.csv

的csv

1 个答案:

答案 0 :(得分:2)

那么你说的话

1。读取CSV

df <-read.csv("product.csv",header=T,sep=";")

2。如果需要,转换日期并订购

df$Date<-as.Date(as.character(df$Date),"%Y%m%d" )
df = df[with(df, order(Date)),] 

3。添加二进制列

library(dplyr)

df2<-df %>% mutate(Flag=ifelse( 
(lag(PVP,n =1)<=.75*PVP & as.numeric(Date-lag(Date,n=1))<=3) |
(lag(PVP,n =2)<=.75*PVP & as.numeric(Date-lag(Date,n=2))<=3) |
(lag(PVP,n =3)<=.75*PVP & as.numeric(Date-lag(Date,n=3))<=3) ,1,0))

4。输出

filter(df2,Flag==1)

    Date Quantity     PVP Flag
1  2007-06-06       10   1.690    1
2  2007-06-07       10   1.690    1
3  2007-06-08       26   1.690    1
4  2008-04-16        3   2.490    1
5  2008-04-17        4   2.490    1
6  2008-04-18       16   2.490    1
7  2010-03-17        9   1.990    1
8  2010-03-18       17   1.990    1
9  2010-03-19       12   1.990    1
10 2010-03-22       13   1.990    1
11 2010-03-23       10   1.990    1
12 2010-03-24       21   1.990    1
13 2010-06-02       15 174.125    1
14 2010-08-04       20 174.125    1
15 2011-05-18        8   1.990    1
16 2011-06-01       10   1.990    1
17 2011-06-02        9   1.990    1
18 2011-06-03       14   1.990    1
19 2012-05-11       10   2.170    1
20 2012-05-12       12   2.170    1
21 2012-05-13        6   2.170    1

5.让它看起来更好

is.discount=function(dataframe=df,date="Date",argument="PVP",discount=.75,nbday=3){
         result<-0
         for (i in seq(1,nbday,1)){
                 result<- result | (lag(dataframe[,argument],n=i)<=discount*dataframe[,argument] & as.numeric(dataframe[,date]-lag(dataframe[,date],n=i))<=nbday)
             }
         return (result)
     }


df3<-df %>% mutate(Flag=ifelse( is.discount() ,1,0))

 all.equal(df2,df3)
 TRUE

我希望它有所帮助。