[R] (list) object cannot be coerced to type 'double'
Muhammad Rahiz
muhammad.rahiz at ouce.ox.ac.uk
Tue Jan 26 17:42:54 CET 2010
Dear all,
My script returns the following error:
Error in storage.mode(test) <- "logical" :
(list) object cannot be coerced to type 'double'
I've looked around and some suggest that it could be the way data have been imported. For my case, I don't think it is such. Rather I think it has to be the way I define x1 and in1. x1 is an object in which the values are written to which in turn serve as input.
The object, x, contain 32 files of 2x2 matrix.
Any guidance would be appreciated.
Muhammad
---------------------------------------------
# Cleans workspace
rm(list=ls())
# ---------- DATA ---------- #
# Load DATA[1]: Monthly anomalies
f <- list.files("C:/Documents and Settings/shil3148/Desktop/dsi",
"\\.txt$") # List files with pattern matching
x <- lapply(f, read.table) # DATA[1]: Monthly anomalies
# Prepare DATA[2]: Rolling Sum
nt <- 6
ed <- nt - 1
fl <- length(x)
y0 <- list()
for (i in 1:(fl-ed)) # Remove NA values
{
y0[[i]] <- Reduce("+", x[c(i:(i+ed))]) # RAW: Rolling Sum
}
# Data conversion
xx <- list()
for (z in 1:length(x))
{
xx[[z]] <- as.matrix(x[[z]]) # convert data to matrix
}
yy <- list()
for (f in 1:length(y0))
{
yy[[f]] <- as.matrix(y0[[f]]) # convert data to matrix
}
# ---------- ALGORITHM ---------- #
x1 <- list() # Error here
in1 <- list() # Error here
for (t in 1:27) # This is the length of yy
{
in1[t+1] <-
ifelse(x1[t] < 0, (
ifelse(yy[t] < 0, x1[t] + xx[t+5], 0)), (
ifelse(yy[t+1] >= 0, 0, (
ifelse(xx[t+5] >= 0, 0, xx[t+5])
))
)
)
x1[t+1] <- in1[t+1]
}
More information about the R-help
mailing list