ệ log' not Meaningful for factors

Asked

Viewed 607 times

2

I tried to run an example, but gave the error below:

Calculate asset returns log

r <- diff(log(prices)) Error in Math.factor(prices) n <- length(r) Error: Object 'r' not found

Someone knows how to fix it?

  • 1

    its price vector is of the type factor and not of the numeric type as it is necessary to use the function log. My guess is you read the base and forgot to use the argument dec = "," to specify that your decimal separator is ,.

  • Thank you very much Daniel! I transformed the data into numerics by the function: prices <- as.Nonumeric(as.Haracter(prices))

  • please post the reply @JPA, or who helped you

1 answer

1

‘log’ not meaningful for factors

means that you tried to apply the logarithm to an object of the type fator.

Example of the problem:

nn <- factor("4233")
log(nn)
Error in Math.factor(nn) : ‘log’ not meaningful for factors

Quick fix in this case:

log(as.numeric(as.character(nn)))
[1] 8.350666

Example of Other Errors:

b <- factor("Caracter")
log(b)
Error in Math.factor(b) : ‘log’ not meaningful for factors

Note that if you try to apply the log a character also will not rotate:

a <- "Caracter"
log(a)
Error in log(a) : non-numeric argument to mathematical function

Only numeric type objects can be logged:

c <- 3
log(c)
[1] 1.098612

Browser other questions tagged

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