Df2Xts: Convert a Dataframe to an xts object or Vice Versa

Description Usage Arguments Value Examples

Description

Of xcast, it is a wrapper over the R CRAN package zoocat function cast2zoocat Convert a Dataframe to an xts object.

Of melt, convert an xts object to a data.frame. Expects an imported S3 "melt" function of the form of reshape2::melt.

Of meltXts2Df, it is a wrapper over the R CRAN package zoocat function melt. Convert an xts object to a dataframe.

Of indName, it is a wrapper over the R CRAN package zoocat function indname. Get the name of the index variable.

Of 'indName<-', is a wrapper over the R CRAN package zoocat function 'indname<-'. Set the name of the index variable.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
xcast(
  x,
  IndexVar = NULL,
  ValueVar = NULL,
  AttrVar = NULL,
  FunAggr = NULL,
  DelUniqCAttr = F,
  RetIndexClass = NULL
)

## S3 method for class 'xts'
melt(
  data,
  value.name = "value",
  index.name = indName(data),
  na.rm = FALSE,
  ...
)

meltXts2Df(x, ValueName = "value", IndexName = indName(data), RmNA = F, ...)

indName(x)

indName(x) <- value

Arguments

x

Of xcast, a dataframe. Of melt.xts, an xts object. Of indname and 'IndName<-' an xts object. Default is NULL. Required.

IndexVar

String. Default is NULL. Required. The name of the column to be treated as the index of the xts object. Different from zoocat::cast2zoocat, this can be composed of two or three columns of the column names: c("year", "month") or c("year", "month", "day").

ValueVar

String. Default is NULL. Required. The name of the column which stored the values.

AttrVar

String. Default is NULL. The name of the column which will be used as column attributes of the xts object. If NULL, all columns except ValueVar and IndexVar will be used.

FunAggr

Function. Default is NULL. Aggregation function needed if variables do not identify a single observation for each output cell. Defaults to length (with a message) if needed but not specified. See ? reshape2::dcast.

DelUniqCAttr

Logical. Default is TRUE. If TRUE, the column attributes with unique value will be deleted.

RetIndexClass

String. Default is "POSIXct". Attempt to return the xts with the index of this class. If ValueVar is a single column and not of one of the R base types: integer, numeric, logical or character, then the function will try to return the original ValueVar non-R base type e.g. (Date or POSIXct).

data

Xts object. Default is NULL. Required. Xts object to melt.

value.name

String. Default is NULL. The name of the column to store values.

index.name

String. String. Default is the result of the function call indName(data).The name of the column used to store the index of the xts object.

na.rm

Logical. Default is FALSE. As in R CRAN package reshape2 function melt. Should NA values be removed from the data set?

...

Dots Passed.

ValueName

String. Default is "value". The name of the column to store values..

IndexName

String. Default is the result of the function call indName(x). The name of the column used to store the index of the xts object.

RmNA

Logical. Default is FALSE. As in R CRAN package reshape2 function melt. Should NA values be removed from the data set?

value

String. Default is NULL. Required. The new value.

Value

Of xcast, an xts object.

Of melt.xts, a dataframe.

Of meltXts2Df, a dataframe.

Of indName, String. Index variable name.

Of 'indName<-', silently set index variable name.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## Not run: 
df <- data.frame(year = rep(1991 : 1995, each = 24), month = rep(1 : 12, 10),
                 varname = rep(c('a', 'b'), each = 12), city = rep(1 : 3, each = 40),
                 value = 1 : 120)

xcast(df, IndexVar = "year", ValueVar = "value")

# FunAggr = length
xcast(df, IndexVar = "year", ValueVar = "value",
  AttrVar = "varname")

xcast(df, IndexVar = "year", ValueVar = "value",
  AttrVar = "varname", FunAggr = sum)

xcast(df, IndexVar = c("year","month"), ValueVar = "value")

xcast(df, IndexVar = "year", ValueVar = "value", RetIndexClass = "Date")

casted <- xcast(df, IndexVar = c("year", "month"), ValueVar = "value",
  RetIndexClass = "Date")

meltXts2Df(casted)

dg <- df
dg[["TimeDate"]] <- zoo::as.Date(paste0(df$year, "-",DescTools::StrPad(df$month, width = 2, pad = "0", adj = "right"), "-", "01"))
dg$year <- NULL; dg$month <- NULL
xcast(dg, IndexVar = "TimeDate", ValueVar = "value")
# in out, expecting an index of class "Date"


## End(Not run)

AndreMikulec/econModel documentation built on June 30, 2021, 9:48 a.m.