I have a data.frame where I want to remove rows which have consecutive days. For example, I have the following data.frame (head), its name is sell_tv and I want to remove the rows which have consecutive dates. In this particular case I want to remove row 5, as row 5 & 6 have consecutive dates.
Date Open High Low Close Sell.TV Buy.TV
1 2015-04-08 2207 2204 2165 2166 4.038113 3.083603
2 2015-03-16 2214 2215 2172 2198 4.041986 3.087017
3 2015-03-05 2343 2364 2320 2324 4.023689 3.081034
4 2015-01-27 2171 2182 2151 2178 4.021998 3.070200
5 2015-01-23 2234 2244 2222 2230 4.032086 3.061206
6 2015-01-22 2278 2282 2242 2246 4.037248 3.095450
I have written the following code for this but getting:
****"Error in if (sell_tv$Date[i] == sell_tv$Date[i + 1] + 1) { : missing value where TRUE/FALSE needed"****
Code :
for( i in 1:nrow(sell_tv))
{
if (sell_tv$Date[i] == sell_tv$Date[i+1] + 1 )
{
new_sell<- sell_tv[-i,]
}
else
{
new_sell<- sell_tv[,]
}
i= i+1
}
Thankful for any help!
ibecauseRwill do that for you (because that's how niceRis ;-)). Theni+1when i isnrow(sell_tv)will not correspond to an existing row. One way to go, keeping the basis of your code, could be to create a variable where you can put the numbers of the rows that are to be deleted and then you can delete them all at oncesell_tv[ c(9999,diff(sell_tv$Date)) != -1, ]