r - set dimnames of an xts object with the names of certain elements in a list through loop -


my question part of function coding.

i've got list of data.frames (each own numeric , date columns) have either weekly data or monthly data. i've defined 2 empty objects "weekly" , "monthly" loop have written goes through , finds out element weekly or monthly. then, loop categorises each element weekly/monthly , converts them xts object.

library(xts) weekly = null monthly = null (i in 1:length(terms.list)) { diff.days <- difftime(as.date(terms.list[[i]][2,1]), as.date(terms.list[[i]][1,1])) if (diff.days < 10) {   freq <- 52   weekly = cbind(weekly, xts(terms.list[[i]][,2], order.by=terms.list[[i]][[1]], frequency=freq))   } else {   freq <- 12   monthly = cbind(monthly, xts(terms.list[[i]][,2], order.by=terms.list[[i]][[1]], frequency=freq))   }} 

so end 2 xts objects i.e. weekly , monthly. want include in loop line of code picks out colnames of weekly defined terms (and monthly defined terms) , set dimnames of each type of xts object respectively.

something like:

dimnames(weekly)[[2]] <- names(terms.list[i]) dimnames(monthly)[[2]] <- names(terms.list[i]) 

note want in loop. i've tried keeps on giving me following errors:

error in `colnames<-`(`*tmp*`, value = "jobs") :  length of 'dimnames' [2] not equal array extent  

"jobs" 1 of data.frame elements names.

any assistance appreciated.

i couldn't test this, because didn't provide reproducible example, think should work. basically, need construct dimname list , set in xts constructor.

library(xts) weekly <- null monthly <- null (i in seq_along(terms.list)) {   term <- terms.list[[i]]   dimnames <- list(null, names(terms.list)[i])   diff.days <- difftime(as.date(term[2,1]), as.date(term[1,1]))   if (diff.days < 10) {     freq <- 52     weekly <- cbind(weekly, xts(term[,2], term[,1], freq, dimnames=dimnames))   } else {     freq <- 12     monthly <- cbind(monthly, xts(term[,2], term[,1], freq, dimnames=dimnames))   } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -