Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zoo column name for single column object

Tags:

r

zoo

I have a question on column names in zoo. I usually create zoo objects from a data frame, and I pick up the column(s) from the data frame to be the zoo column(s). What I found is that, if I only specify one column to the zoo object, then the column name will not be taken by zoo. Does that mean it is not considered a "column" in zoo?

Here is an example how I usually do it, with one and two columns.

Lines.1 = "Index,dbt
2008-08-20 15:03:18,88.74
2008-08-20 15:08:18,88.74
2008-08-20 15:13:18,86.56
2008-08-20 15:18:18,85.82"

Lines.2 = "Index,dbt,rh
2008-08-20 15:03:18,88.74,18.25
2008-08-20 15:08:18,88.74,17.25
2008-08-20 15:13:18,86.56,18.75
2008-08-20 15:18:18,85.82,19.75"

x =read.table(text = Lines.1, header = TRUE, sep = ",")
y =read.table(text = Lines.2, header = TRUE, sep = ",")

colnames(x)
colnames(y)

library(zoo)
zx = zoo(x[,2], as.POSIXct(x$Index, tz="GMT"))
zy = zoo(y[,2:3], as.POSIXct(y$Index, tz="GMT"))

colnames(zx)
colnames(zy)

The result shows as follows:

> colnames(zx)
NULL
> colnames(zy)
[1] "dbt" "rh" 

Do I miss something?

like image 887
ery Avatar asked Jul 02 '13 15:07

ery


People also ask

How do I get column names in Rstudio?

To access a specific column in a dataframe by name, you use the $ operator in the form df$name where df is the name of the dataframe, and name is the name of the column you are interested in. This operation will then return the column you want as a vector.

How do you name columns in a Dataframe in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

What does zoo mean in R?

zoo is an R package providing an S3 class with methods for indexed totally ordered observations, such as discrete irregular time series. Its key design goals are independence of a particular index/time/date class and consistency with base R and the "ts" class for regular time series.


1 Answers

This is the default behaviour of [ when used with arrays or data frames; empty dimensions are dropped. Consider

> x[, 2]
[1] 88.74 88.74 86.56 85.82
> class(x[,2])
[1] "numeric"
> is.data.frame(x[,2])
[1] FALSE

In this case the 1-column data frame doesn't need information about which column it is and hence R drops that information and returns the contents of the column as a numeric (in this case) vector, as can be seen above. That vector doesn't have a colname attribute and hence zoo has nothing to work with.

A solution is to use drop = FALSE in the index x[, 2, drop = FALSE] as in

> zx <- zoo(x[, 2, drop = FALSE], as.POSIXct(x$Index, tz="GMT"))
> zx
                      dbt
2008-08-20 15:03:18 88.74
2008-08-20 15:08:18 88.74
2008-08-20 15:13:18 86.56
2008-08-20 15:18:18 85.82

To see why/how this works, look at

> x[, 2, drop = FALSE]
    dbt
1 88.74
2 88.74
3 86.56
4 85.82
> is.data.frame(x[, 2, drop = FALSE])
[1] TRUE

And note the lack of colnames when the default (TRUE) is used in the [ index:

> colnames(x[, 2, drop = FALSE])
[1] "dbt"
> colnames(x[, 2, drop = TRUE])
NULL

Now read ?'[' for more details.

like image 77
Gavin Simpson Avatar answered Nov 04 '22 22:11

Gavin Simpson