R Convert Between Zoo Object and Data Frame, Results Inconsistent for Different Numbers of Columns

R convert between zoo object and data frame, results inconsistent for different numbers of columns?

To convert from data frame to zoo use read.zoo:

library(zoo)
z <- read.zoo(df)

Also note the availability of the drop and other arguments in ?read.zoo .

and to convert from zoo to data frame, including the index, use fortify.zoo:

fortify.zoo(z, name = "Date")

(If ggplot2 is loaded then you can just use fortify.)

As mentioned in the comments below the question, the question as well as some of the other answers are either outdated or have some significant misunderstandings. Suggest you review https://cran.r-project.org/web/packages/zoo/vignettes/zoo-design.pdf which discusses the design philosophy of zoo which includes consistency with R itself. Certainly zoo would be a lot harder to use if you had to remember one set of defaults for R and another for zoo.

Copy columns of zoo object on the basis of a condition in data frame

May be

indx <- d1$DoApply=='YES'
indx1 <- d1$Name[indx] %in% names(z)
z[, indx1] <- z[, d1$Base[indx][indx1]]
z
# T1 T2 T3
#2013-01-18 21 21 21
#2013-01-20 17 17 17
#2013-01-21 24 24 24
#2013-01-22 15 15 15
#2013-01-23 18 18 18

In R, how do I maintain column names through by and zoo?

The problem is that merge.zoo will drop names for a univariate zoo by default.

This behaviour can be overridden by passing drop = F to the args list. We can use a partial function application of merge.zoo to achieve this when using do.call:

f <- function(x) {

zoo(x$Value, order.by = x$Date)
}

s <- do.call(partial(merge.zoo, drop = F), by(d, factor(d$Label), f))

or without partial:

s <- do.call(merge.zoo, append(by(d, factor(d$Label), f), list(drop = F)))

For d <- d[d$Label == "A", ] this gives:

           A
2015-03-21 1
2015-03-22 2
2015-03-23 7

Note that the pryr package is required for partial function application.

Merge two zoo objects with data summed up for column with same names

Calculate the common names and the names unique to each input and then put it all together fixing up any mangled names:

both <- intersect(names(z1), names(z2))
only1 <- setdiff(names(z1), both)
only2 <- setdiff(names(z2), both)

setNames(cbind(z1[, only1], z2[, only2], z1[, both] + z2[, both]),
c(only1, only2, both))

This works at least on the sample input. There may or may not need to be some changes if your actual problem varies from it in some important way.

In R, how do I maintain column names through by and zoo?

The problem is that merge.zoo will drop names for a univariate zoo by default.

This behaviour can be overridden by passing drop = F to the args list. We can use a partial function application of merge.zoo to achieve this when using do.call:

f <- function(x) {

zoo(x$Value, order.by = x$Date)
}

s <- do.call(partial(merge.zoo, drop = F), by(d, factor(d$Label), f))

or without partial:

s <- do.call(merge.zoo, append(by(d, factor(d$Label), f), list(drop = F)))

For d <- d[d$Label == "A", ] this gives:

           A
2015-03-21 1
2015-03-22 2
2015-03-23 7

Note that the pryr package is required for partial function application.

Merge two zoo objects with data summed up for column with same names

Calculate the common names and the names unique to each input and then put it all together fixing up any mangled names:

both <- intersect(names(z1), names(z2))
only1 <- setdiff(names(z1), both)
only2 <- setdiff(names(z2), both)

setNames(cbind(z1[, only1], z2[, only2], z1[, both] + z2[, both]),
c(only1, only2, both))

This works at least on the sample input. There may or may not need to be some changes if your actual problem varies from it in some important way.

Converting CRSP Daily Stock Returns into Zoo Object in R

Use read.zoo with the split argument:

read.zoo(df, split = "Z.firm")

giving this "zoo" object:

                     A          B          C
2004-03-24 0.54966989 -0.2778645 0.5547522
2004-03-25 -0.84160374 0.3608284 -0.4986355
2004-03-26 0.03299794 -0.5909124 0.1957338
2004-03-27 0.52414971 0.9755906 -0.4555405
2004-03-28 -1.72760411 -1.4457499 -0.3628555
2004-03-29 NA 0.2952068 NA

zoo create new column with dynamic column name

Try setNames :

setNames( merge(test, 0), c(names(test), paste0("x", 1)) )

or names<-.zoo like this:

test2 <- merge(test, 0)
names(test2) <- c(names(test), paste0("x", 1))


Related Topics



Leave a reply



Submit