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

** **

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

** **

** **

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

** **

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

** **

** **

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

** **

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

** **

** **

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

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

**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

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