[R] Remove Columns by Name from zoo object

Gabor Grothendieck ggrothendieck at gmail.com
Mon Nov 10 13:35:29 CET 2008


On Mon, Nov 10, 2008 at 12:31 AM, Sean Carmody <seancarmody at gmail.com> wrote:
> The tricks for removing columns specified by name from data frames such as
>
> x$mycol <- NULL

That only works for data frames since they are based on
lists but not for objects like matrix, ts and zoo which are not
based on lists.  Try this:

library(zoo)
z <- zoo(cbind(a = 1:2, b = 3:4, c = 5:6))
# all but b
z <- z[, colnames(z) != "b"]
z

z <- zoo(cbind(a = 1:2, b = 3:4, c = 5:6))
# all but b and c
z <- z[, ! colnames(z) %in% c("b", "c")]
z



More information about the R-help mailing list