The tricks for removing columns specified by name from data frames such as x$mycol <- NULL (and others described here http://wiki.r-project.org/rwiki/doku.php?id=tips:data-frames:remove_columns_by_name) do not seem to work for a zoo object. Any suggestions as to how to do this, or is my best bet to coerce to a data.frame, remove the column and coerce back to zoo? -- Sean Carmody The Stubborn Mule http://www.stubbornmule.net http://twitter.com/seancarmody
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 <- NULLThat 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
Perfect, works like a charm. Thanks Gabor. Sean. On Mon, Nov 10, 2008 at 11:35 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> 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 >-- Sean Carmody The Stubborn Mule http://www.stubbornmule.net http://twitter.com/seancarmody