r - Possible bug: setting colnames of an xts object in the last line of a function -
i have r function follows form:
creatextsfromfile <- function(file) { data <- read.table(file, sep = ",") data <- xts(data[, 1], order.by = data[, 1], unique = false) # manipulate data colnames(data) <- c("a", "b", "c") }
however, calling function returns character[3]:
data <- creatextsfromfile("file.txt") str(test) chr [1:2] "a" "b"
however, after removing final line in function, same call returns xts. strange thing moving final line other line in function returns xts. has come across before? did search on https://bugs.r-project.org/bugzilla3/ didn't find anything.
this not bug. help("function")
tells happening. says, "if end of function reached without calling return, value of last evaluated expression returned."
so need return data
object:
creatextsfromfile <- function(file) { data <- read.table(file, sep = ",") data <- xts(data[, 1], order.by = data[, 1], unique = false) # manipulate data colnames(data) <- c("a", "b", "c") return(data) }
also, use as.xts(read.zoo(...))
instead of writing new function scratch.
Comments
Post a Comment