Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

r - Delete rows from a data table depending from the values on the previous one

I have a dataframe similar to the following one, organized by date:

|Symbol |   Date    | volume |price |
|------------------------------------
|A      |2014-01-01 | 1      |   5  |
|A      |2014-01-02 | 3      |   8  |
|A      |2014-01-03 | 7      |   4  |
|A      |2014-01-05 | 0      |  4   |
|A      |2014-01-06 |0       |   4  |
|A      |2014-01-07 |3       |   6  |
|A      |2014-01-08 |34      |   7  |
|A      |2014-01-09 |45      |  34  |
|A      |2014-01-10 |4       |   5  | 
|A      |2014-01-11 |9       |   7  |
|A      |2014-01-12 |0       |   7  |
|A      |2014-01-13 |0       |   7  | 
|A      |2014-01-14 |8       |   6  |
|A      |2014-01-15 |4       |   4  |
|A      |2014-01-16 |0       |   7  |
|A      |2014-01-17 |4       |   7  |

And I need to delete the rows that simultaneously satisfy that volume=0 and that in the price column have a value exactly equal to that of the previous row. To obtain a data frame like the following:

|Symbol |   Date    | volume |price |
|------------------------------------
|A      |2014-01-01 | 1      |   5  |
|A      |2014-01-02 | 3      |   8  |
|A      |2014-01-03 | 7      |   4  |
|A      |2014-01-07 |3       |   6  |
|A      |2014-01-08 |34      |   7  |
|A      |2014-01-09 |45      |  34  |
|A      |2014-01-10 |4       |   5  | 
|A      |2014-01-11 |9       |   7  |
|A      |2014-01-14 |8       |   6  |
|A      |2014-01-15 |4       |   4  |
|A      |2014-01-16 |0       |   7  |
|A      |2014-01-17 |4       |   7  |

I guess this should be done with a for loop, but I really don't know how to do it. I am still very new to R. I hope you can help me.

question from:https://stackoverflow.com/questions/65893552/delete-rows-from-a-data-table-depending-from-the-values-on-the-previous-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A base R option with subset

> subset(df,!(volume==0 & c(TRUE,diff(price)==0)))
   Symbol       Date volume price
1       A 2014-01-01      1     5
2       A 2014-01-02      3     8
3       A 2014-01-03      7     4
6       A 2014-01-07      3     6
7       A 2014-01-08     34     7
8       A 2014-01-09     45    34
9       A 2014-01-10      4     5
10      A 2014-01-11      9     7
13      A 2014-01-14      8     6
14      A 2014-01-15      4     4
15      A 2014-01-16      0     7
16      A 2014-01-17      4     7

A data.table option

> setDT(df)[!(volume==0 & c(TRUE,diff(price)==0))]
    Symbol       Date volume price
 1:      A 2014-01-01      1     5
 2:      A 2014-01-02      3     8
 3:      A 2014-01-03      7     4
 4:      A 2014-01-07      3     6
 5:      A 2014-01-08     34     7
 6:      A 2014-01-09     45    34
 7:      A 2014-01-10      4     5
 8:      A 2014-01-11      9     7
 9:      A 2014-01-14      8     6
10:      A 2014-01-15      4     4
11:      A 2014-01-16      0     7
12:      A 2014-01-17      4     7

Data

> dput(df)
structure(list(Symbol = c("A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A"), Date = c("2014-01-01",
"2014-01-02", "2014-01-03", "2014-01-05", "2014-01-06", "2014-01-07",
"2014-01-08", "2014-01-09", "2014-01-10", "2014-01-11", "2014-01-12",
"2014-01-13", "2014-01-14", "2014-01-15", "2014-01-16", "2014-01-17"
), volume = c(1L, 3L, 7L, 0L, 0L, 3L, 34L, 45L, 4L, 9L, 0L, 0L,
8L, 4L, 0L, 4L), price = c(5L, 8L, 4L, 4L, 4L, 6L, 7L, 34L, 5L,
7L, 7L, 7L, 6L, 4L, 7L, 7L)), class = "data.frame", row.names = c(NA,
-16L))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...