Toolbox
Toolbox

Reputation: 2493

xts - Delete specific rows without transform to other format

Question: How can I delete specific rows in xts, without the need of transform to other formats. The selected row to be deleted, will be based on the row number, or [column.one] but not the timestamp.

This is what I have tested so far:

Send row 4, column 1, to NULL

myxts[4,1] <- NULL 

Result: Error in NextMethod(.Generic) : number of items to replace is not a multiple of replacement length

Add value to NA, then delete all NA with [na.omit]

xts3[1,1] <- NA # Adds NA

na.omit(xts3)

Result: Does remove the NA rows but does not solve the straightforward way of deleting a row.


This is my test xts:

##########
# Test xts
##########
dates <- as.POSIXct( # Construct the dates to be used.
  c(
    "2013-07-24 09:01:00", 
    "2013-07-24 09:02:00", 
    "2013-07-24 09:03:00"
  )
)

column.one <- c(1,2,3) # Data in column.one.
data <- data.frame(column.one) # Create a dataframe.
xts3 <- xts(x=data, order.by=dates) # Create xts based on dataframe.

Upvotes: 0

Views: 2289

Answers (1)

phiver
phiver

Reputation: 23608

The following lines result in equal output.

xts3[-2, ]

xts3[index(xts3) != index(xts3[xts3$column.one == 2])]

                    column.one
2013-07-24 09:01:00          1
2013-07-24 09:03:00          3

But for xts / zoo timeseries it is better and safer to work with the indexes as this leads to a finer control of what you want / can achieve with them.

Upvotes: 1

Related Questions