How to rename a column in a ts object - r

I am working with objects of class ts in R.
Is there code I can use to change column names in this kind of an object?
For a data frame, I would use something like this:
Shipper_City <- rename(Shipper_City,"ShipCity_Old" = "ShipCity")

Use the 'colnames()' function
You want to use colnames(), because you're dealing with a matrix in case of a ts object:
colnames(data) <- c("ColName1", "ColName2")
Hope this helps.

Here is a function that I created that might be useful for you.
rename.ts <- function(ts, ...){
if (inherits(ts, "mts")) {
x <- list(...)
old_names <- names(x)
if (all(old_names %in% colnames(ts))) {
id_old_names <- which(old_names %in% colnames(ts))
colnames(ts)[id_old_names] <- unname(unlist(x))
} else {
stop("You must provide valid column names")
}
} else {
stop("You must provide a mts object as argument")
}
ts
}
# Example
mts <- ts(data = mtcars, start = 2013, frequency = 4)
rename.ts(mts, "mpg" = "mpg2", "cyl" = "cyl2")

Related

Generic S3 function for multiple classes

I have a generic function like this:
convert <- function(x) UseMethod("simplifyResultConvert")
convert.default <- function(x) {
x
}
convert.POSIXct <- function(x) {
as.character(x)
}
convert.factor <- function(x) {
as.character(x)
}
convert.Date <- function(x) {
as.character(x)
}
Is there any way to simplify it by making one generic for type: POSIXct, Date and factor?
To make it clear: I need something like:
convert.(POSIXct || factor || date) <- funciton(x) {as.character(x)}
Write it like this:
convert.default <- function(x) x
convert.Date <-
convert.factor <-
convert.POSIXct <- function(x) as.character(x)
A further simplification would be:
convert.default <- identity
convert.Date <-
convert.factor <-
convert.POSIXct <- as.character
You can put a test of the class in the default method, for example:
convert.default <- function(x) {
if (inherits(x, "POSIXct") ||
inherits(x, "factor") ||
inherits(x, "Date"))
as.character(x)
else
x
}
This is not quite the same as what you want, because an object could have class c("POSIXct", "other") and if there was a convert.other() method set, it would be called instead of the default.
If you really want the behaviour you asked for, you need the three functions, but you can save a bit of typing by using
convert.Date <- convert.factor <- convert.POSIXct <- function(x) {
as.character(x)
}
Since there are no super-classes in S3, this is not possible with S3. However, you can easily do this with S4 which allows defining super-classes:
setGeneric("convert", function(object) {
standardGeneric("convert")
})
setClassUnion("fooClasses", members = c("factor", "Date", "POSIXt")) #POSIXt is a super-class
setMethod("convert", signature(object = "fooClasses"), function(object) {
as.character(object)
})
class(convert(as.Date("2010-10-10")))
#[1] "character"
class(convert(as.POSIXct("2010-10-10")))
#[1] "character"

name columns of list element with same name

my code is like the following:
unemp <- c(1:10)
bsp_li <- list(c(1:10),c(11:20),c(21:30))
var_data_rep <- lapply(bsp_li, function(x) {cbind(as.numeric(x), as.numeric(unemp))} )
var_data_rep2 <- lapply(var_data_rep, function(x) {colnames(x) = c("rGDP", "U")} )
but it does not what i wanted. i would like to name always the two elements of the list var_data_rep with c("rGDP", "U"). but instead the values are overwritten by c("rGDP", "U") and becomes the sole elements of the list.. can anyone help? i need the same names because i want to estimate always the same model later.
Easy fix: put the names in as the matrices are created:
var_data_rep <- lapply(bsp_li, function(x) {
cbind(rGDP = as.numeric(x), U = as.numeric(unemp))
} )
More roundabout fix (why your attempt didn't work): functions return the last line. You want to return x, not colnames(x)
var_data_rep2 <- lapply(var_data_rep, function(x) {
colnames(x) = c("rGDP", "U")
return(x)
} )
Fancy fix: use the colnames<- function directly
var_data_rep3 = lapply(var_data_rep, `colnames<-`, c("rGDP", "U"))

call a function from a vector of given functions in R

have the following function:
setTypes <- function(df2, ...) {
fns <- as.list(substitute(list(...)))
for(i in 1:length(df2)) {
if(fns[i] == '') {
next
}
df2[i,] <- fns[i](df2[i,])
}
return(df2)
}
want to do this:
test<-setTypes(sls,c('','as.Date','','','as.numeric','as.numeric'))
idea is to change the types of the fields in a data frame without having to do sls$field <- as.numeric(sls$field) for every field.
I had written a function like this that worked:
fn <- function(t) {
return(t("55.55000"))
}
and the output is this:
> fn(as.numeric)
[1] 55.55
however, i can't figure out why either doing variable length argument as a list and calling it as list[index](input) doesn't work. or even passing a vector of functions like c(as.Date, as.numeric, as.character) and doing c[1]('2015-10-10') # as.Date('2015-10-10')
I am receiving the error 'attempt to apply non-function'.. I've also tried using call but to no avail. Help?
The problem is that class(c[1]) is a list use c[[1]] instead
Example code
v <- c(as.numeric,as.character)
v[[1]]("1")
v[[2]](1)
EDIT
Your example should be:
setTypes <- function(df2, ...) {
fns <- list(...)
for(i in 1:NCOL(df2)) {
if(is.function(fns[[i]])) {
df2[,i] <- fns[[i]](df2[,i])
}
}
return(df2)
}
df <- data.frame(v1 = c(1,2), v2 = c("1","2"))
setTypes(df,as.character,'',as.numeric)

Run Function in Loop in R

I need to use the below function in loop as i have 100s of variables.
binning <- function (df,vars,by=0.1,eout=TRUE,verbose=FALSE) {
for (col in vars) {
breaks <- numeric(0)
if(eout) {
x <- boxplot(df[,col][!df[[col]] %in% boxplot.stats(df[[col]])$out],plot=FALSE)
non_outliers <- df[,col][df[[col]] <= x$stats[5] & df[[col]] >= x$stats[1]]
if (!(min(df[[col]])==min(non_outliers))) {
breaks <- c(breaks, min(df[[col]]))
}
}
breaks <- c(breaks, quantile(if(eout) non_outliers else df[[col]], probs=seq(0,1, by=by)))
if(eout) {
if (!(max(df[[col]])==max(non_outliers))) {
breaks <- c(breaks, max(df[[col]]))
}
}
return (cut(df[[col]],breaks=breaks,include.lowest=TRUE))
}}
It creates a variable with binned score. The naming convention of variable is "the original name" plus "_bin".
data$credit_amount_bin <- iv.binning.simple(data,"credit_amount",eout=FALSE)
I want the function runs for all the NUMERIC variables and store the converted bins variables in a different data frame and name them with "the original name _bin".
Any help would be highly appreciated.
Using your function, you could go via lapply, looping over all values that are numeric.
# some data
dat0 <- data.frame(a=letters[1:10], x=rnorm(10), y=rnorm(10), z=rnorm(10))
# find all numeric by names
vars <- colnames(dat0)[which(sapply(dat0,is.numeric))]
# target data set
dat1 <- as.data.frame( lapply(vars, function(x) binning(dat0,x,eout=FALSE)) )
colnames(dat1) <- paste(vars, "_bin", sep="")
Personally, I would prefer having this function with vector input instead of data frame plus variable names. It might run more efficiently, too.

Symbolically access object created by getSymbols

In the following pseudo-code, I don't know how to identify ?unknown?. I realize that y has the name of the symbol pointed to by asset. The objects for the symbols are stored in the environment. I need to access the object through a variable.
library(quantmod)
x<-c("IBM","GOOG","AAPL")
for (asset in x)
{
y <- getSymbols(asset, src = "yahoo",from = startDate,to = endDate,auto.assign=FALSE)
mydf <- convert ?unknown? to data frame
mydf$time <- time(?unknown?)
#process mydf
#store results in another data frame
}
#display results
I would create a new environnment where I store the variables.
library(quantmod)
x<-c("IBM","GOOG","AAPL")
e = new.env()
getSymbols(x, src = "yahoo",from = startDate,
to = endDate,auto.assign=FALSE,env=e)
Then to process variable inside the new environment you can use eapply :
mydf <- eapply(e,function(asset){
})
With auto.assign=F they are not assigned to an environment variable (that is a behavior that will be deprecated). Now you get an object of class "xts". You can just do
library(quantmod)
startDate <- as.Date("2014-01-01")
endDate <- as.Date("2014-01-10")
x<-c("IBM","AAPL")
dfall<-NULL
for (asset in x)
{
y <- getSymbols(asset, src = "yahoo",
from = startDate,to = endDate,auto.assign=FALSE)
mydf <- as.data.frame(y)
names(mydf)<-c("Open","High","Low","Close","Volume","Adjusted")
mydf$symbol <- asset
mydf$time <- time(y)
if(is.null(dfall)) {
dfall<-mydf
} else {
dfall<-rbind(dfall, mydf)
}
}
dfall

Resources