Log function in R – log(),log2(),log10()

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. basically, log() computes natural logarithms (ln), log10() computes common (i.e., base 10) logarithms, and log2() computes binary (i.e., base 2) logarithms. The general form logb(x, base) computes logarithms with base mentioned. Log() function on getting logarithmic value of a column in R dataframe.

  • log10 function –log10(), computes common logarithms (i.e. base 10)
  • log2 function – log2(), computes binary logarithms (i.e. base 2)
  • log() function – natural logarithm of vector (i.e. base  e)

 

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:

simple log() function computes the natural logarithmic value of number or vector.

# 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:

log() function with base 3 is computed,  the logarithmic value of number or vector to the base 3 is shown below

# 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 with base 2 is computed,  the logarithmic value of number or vector to the base 2 is shown below

# 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 with base 10 is computed,  the logarithmic value of number or vector to the base 10 is shown below

# 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

 

 

 

Example of log() function in the R dataframe

First lets create a dataframe

#### Create dataframe in R

my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                       ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                       Price = c(100.981,80.643,80.223,90.3,65.3,71.9,62.2,71.3,25.1,62.9,41.9,35.7,50.9,121.7))

my_basket
 

so the resultant dataframe will be

round function in R 11

 

 

Log(), Log2(), Log10() function in R dataframe :

log() function takes up the “price” column as argument and computes the natural logarithm value of the column. log2() function takes up the “price” column as argument and computes the logarithm to the base2 value of the column.  log10() function takes up the “price” column as argument and computes the logarithm to the base10 value of the column.

 
#### Log to the base 2 , base10 and natural logarithmic value of the column in R

my_basket$log_base2 = log2(my_basket$Price)
my_basket$log_base10 = log10(my_basket$Price)
my_basket$log_base_e = log(my_basket$Price)
my_basket$log_base3 = log(my_basket$Price, base=3)

so the resultant dataframe with log(), log2(), log10() and log3() calculated on the “price” column will be

log() function in R log2() and log10() 1

 

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

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts