Log function in R – log()

Log function in R –log() computes the natural logarithms (Ln) for a number or vector. Apart from log() function, R also has log10 and log2 functions.

  • log10 function –log10(), computes common logarithms (i.e. base 10)
  • log2 function – log2(), computes binary logarithms (i.e. base 2)

 

Syntax for log function in R:

log(x, base)
  • x – numeric to which log has to be computed
  • base – base of log.

 

Example of log function in R:

# log function in R with natural logarithm

log(6)
output:
[1] 1.791759

 

# log function in R for a vector- natural logarithm

x <- (rep(1:8))
log(x)

natural logarithm for the vector of sequence from 1 to 8 is computed and the output will be

output:
[1] 0.0000000     0.6931472     1.0986123     1.3862944     1.6094379      1.7917595 1.9459101    2.0794415

 

 

Example of log function with specified base in R:

# log function in R with base 3

log(6,3)
output:
[1] 1.63093

 

# log function in R for a vector- with base 3

x <- (rep(1:8))
log(x,3)

log of base 3, for the vector of sequence from 1 to 8 is computed and the output will be

output:
[1] 0.0000000    0.6309298     1.0000000    1.2618595     1.4649735     1.6309298 1.7712437    1.8927893

 

 

Example of log2 function in R:

# log2 function in R

log2(6)
output:
2.584963

 

# log2 function in R for a vector

x <- (rep(1:8))
log2(x)

log2(), for the vector of sequence from 1 to 8 is computed and the output will be

output:
[1] 0.000000     1.000000     1.584963     2.000000     2.321928     2.584963      2.807355 3.000000

 

 

Example of log10 function in R:

# log10 function in R

log10(6)
output:
[1] 0.7781513

 

# log10 function in R for a vector

x <- (rep(1:8))
log10(x)

log10(), for the vector of sequence from 1 to 8 is computed and the output will be

output:
[1] 0.0000000      0.3010300      0.4771213      0.6020600      0.6989700         0.7781513 0.8450980      0.9030900

previous small log function in r                                                                                                                next small log function in r