Problem with "ifelse" - Object cannot be coerced to type 'double

Asked

Viewed 42 times

1

Good afternoon. I’ve been researching the error, but I haven’t found a solution.

I tried to transform the object a but I was unsuccessful.

Any hint?

I have the following problem:

class(ativos)
[1] "xts" "zoo"

model_list <- lapply(ativos, function(x)
lm(BBAS3 ~ x, data = ativos))

resid_list <- lapply(model_list, residuals)
lapply(resid_list, summary)

f <- function(residuals){
  z <- (residuals - mean(residuals))/sd(residuals)
  return(z)}
a <- lapply(resid_list, f)

a_if <- ifelse(a <= -2,
               "Z",
               "N")

Error in ifelse(a <= -2, "Z", "N") : 
(list) object cannot be coerced to type 'double'

Thank you.

EDIT: Sorry for the lack of information.

dput(head(ativos, 5))

structure(c(66124.53, 66967.64, 66712.88, 67975.58, 67814.24, 
17.14, 17.25, 17.38, 18.02, 17.97, 31.18, 31.38, 31.48, 31.86, 
32.89, 26.12, 26.07, 25.87, 26.36, 26.31, 26.86, 26.93, 26.77, 
27.35, 27.26, 27.57, 28.53, 28.16, 28.85, 29.5, 21.8, 23.47, 
22.96, 22.82, 22.61, 43.2, 42.77, 43.2, 43.96, 43.9, 33.95, 33.95, 
33.89, 32.82, 33.14, 13.32, 13.13, 13.03, 13.39, 13.22, 13.16, 
13.05, 13, 13.1, 12.91, 17.28, 17.95, 18.1, 18.3, 18.45, 23.02, 
23.33, 23.12, 23.97, 24.23, 8.66, 8.92, 8.98, 9.61, 9.9, 32.57, 
33.27, 32.35, 33.43, 34.86, 41.95, 41.62, 42.12, 42.55, 42.4, 
27.11, 27.75, 27.34, 28.1, 27.9, 14.14, 13.86, 13.67, 13.67, 
13.41, 0, 0, 0, 0, 0, 9.16, 9.28, 9.35, 9.42, 9.49, 29.72, 29.86, 
29.43, 29.89, 29.72, 22.63, 22.86, 22.17, 22.32, 22.15, 26.7, 
26.75, 25.75, 25.71, 25.32, 18.12, 18.35, 18.31, 18.34, 18.1, 
13.89, 13.85, 13.71, 13.95, 13.92, 57.19, 57.39, 57.5, 58, 59.58, 
14.7, 14.85, 15.02, 15, 14.85, 20.37, 20.88, 20.88, 20.69, 20.64, 
12.89, 13.3, 13.14, 13.36, 13.46, 5.65, 5.71, 5.65, 5.83, 5.96, 
6.95, 7.11, 7.32, 7.8, 8.22, 28.59, 28.8, 28.46, 29.04, 27.8, 
30.95, 31.54, 31.3, 31.68, 31.2, 0, 0, 0, 0, 0, 8.84, 8.82, 8.78, 
9.12, 9.09, 23.43, 23.17, 23.11, 23.92, 23.65, 12.06, 12.15, 
12.01, 12.2, 12.18, 15.29, 15.28, 15.56, 16.06, 15.8, 13.08, 
13.1, 13.36, 13.6, 13.55, 17, 17, 16.8, 16.2, 16.3, 22.47, 22.57, 
23.09, 23.54, 23.54, 17.29, 19, 18.69, 19.25, 19.5, 7.2, 7.38, 
7.34, 7.35, 7.22, 14, 13.72, 13.72, 13.83, 13.9, 22.42, 22.42, 
22.23, 22.39, 22, 26.56, 26.94, 26.68, 27.17, 26.82, 57.91, 57.53, 
57.39, 58.91, 58.45, 16.35, 16.56, 16.81, 16.86, 16.97, 15.58, 
15.62, 15.82, 15.84, 15.86, 19.9, 20.55, 20.18, 20.1, 19.95, 
64, 63.24, 63.57, 65.99, 64.65, 8.34, 8.7, 8.55, 8.74, 8.8, 12.35, 
12.35, 12.86, 12.93, 12.62, 34.49, 35.56, 34.25, 35.7, 34.89, 
32.86, 33.99, 33.3, 34.38, 33.76, 53.1, 54.7, 54.32, 56.38, 59.25, 
20.56, 20.14, 19.7, 20, 20.39, 21.3, 21.86, 21.45, 21.54, 21.83, 
9.68, 9.6, 9.69, 9.94, 9.82, 67.01, 67, 66.6, 68.81, 68.69, 5.37, 
5.44, 5.45, 5.52, 5.46, 32.8, 35.81, 34.4, 33.82, 33.77, 47.6, 
47.5, 46.95, 47.28, 46.7, 3.71, 3.75, 3.7, 3.62, 3.65, 0, 0, 
0, 0, 0, 12.55, 12.65, 12.61, 13.09, 12.88), class = c("xts", 
"zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(1486684800, 
1486944000, 1487030400, 1487116800, 1487203200), tzone = "UTC", tclass = "Date"), .Dim = c(5L, 
66L), .Dimnames = list(NULL, c("IBOV", "ABEV3", "BBAS3", "BBDC3", 
"BBDC4", "BBSE3", "BRAP4", "BRFS3", "BRKM5", "BRML3", "BTOW3", 
"CCRO3", "CIEL3", "CMIG4", "CPLE6", "CSAN3", "CVCB3", "CYRE3", 
"DOLFUT", "ECOR3", "EGIE3", "ELET3", "ELET6", "EMBR3", "ENBR3", 
"EQTL3", "ESTC3", "FLRY3", "GGBR4", "GOAU4", "GOLL4", "HYPE3", 
"IGTA3", "INDFUT", "ITSA4", "ITUB3", "JBSS3", "KLBN11", "KROT3", 
"LAME4", "LREN3", "MGLU3", "MRFG3", "MRVE3", "MULT3", "NATU3", 
"PCAR4", "PETR3", "PETR4", "QUAL3", "RADL3", "RAIL3", "RENT3", 
"SANB11", "SBSP3", "SMLS3", "SUZB3", "TAEE11", "TIMP3", "UGPA3", 
"USIM5", "VALE3", "VIVT4", "VVAR3", "WDOFUT", "WEGE3")))```
  • 1

    Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

  • Can you please, edit the question with the departure of dput(ativos) or, if the base is too large, dput(head(ativos, 20))?

  • 1

    I apologize for the quality of the question, I have added information to make it more reproducible.

1 answer

2

The object a is class "list" because it is the result of lapply of the previous instruction. So just apply lapply once again.

a_if <- lapply(a, function(.a) ifelse(.a <= -2, "Z", "N"))

If you want to have a form other than a list, see, for example, sapply.

a_if2 <- sapply(a, function(.a) ifelse(.a <= -2, "Z", "N"))
class(a_if2)
#[1] "matrix"
  • ,thank you very much for the reply. It is possible to use the same code a_if <- lapply(a, function(.a) ifelse(.a <= -2, "Z", "N")) to reproduce another ifelsebut with the "-2" positive this time?

  • @Gabrielguilherme It is not only change the value in the instruction to the new value (positive)?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.