# How to find the log10 of each value in columns if some columns are categorical in R data frame?

To find the log10 of each value if some columns are categorical in R data frame, we can follow the below steps −

• First of all, create a data frame.

• Then, use numcolwise function from plyr package to find the log10 if some columns are categorical.

## Example

#### Create the data frame

Let’s create a data frame as shown below −

Level<-sample(c("low","medium","high"),25,replace=TRUE)
Group<-sample(c("first","second"),25,replace=TRUE)
Score<-sample(1:50,25)
Demand<-sample(1:100,25)
df<-data.frame(Level,Group,Score,Demand)
df

## Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   Level  Group   Score Demand
1  low    second  30      9
2  high   first   42     48
3  medium first   14     88
4  high   second  27     63
5  high   first   37     97
6  medium first   38     39
7  high   second  13     18
8  high   first   16     14
9  low    second  40     90
10 medium first   25     53
11 low    second  48     78
12 high   second  28     64
13 low    first   22     32
14 medium first    1     31
15 high   first    2     21
16 low    second  17     26
17 high   first    5     41
18 medium first   49     85
19 low    second  45     29
20 low    first    9     49
21 medium first   31     44
22 medium first   29     46
23 high   second  10     36
24 low    second  21     33
25 low    second  35     40

Find the log10 if some columns are categorical

Using numcolwise function from plyr package to find the log10 of each value in numerical columns in the data frame df −

Level<-sample(c("low","medium","high"),25,replace=TRUE)
Group<-sample(c("first","second"),25,replace=TRUE)
Score<-sample(1:50,25)
Demand<-sample(1:100,25)
df<-data.frame(Level,Group,Score,Demand)
library(plyr)
numcolwise(log10)(df)

## Output

     Score     Demand
1  1.2552725 1.851258
2  1.3424227 1.477121
3  1.6334685 1.041393
4  1.6020600 1.986772
5  1.6532125 1.944483
6  1.5910646 1.934498
7  1.5440680 1.897627
8  1.2787536 1.977724
9  1.4913617 1.857332
10 1.5051500 0.602060
11 1.5682017 1.505150
12 1.3979400 1.230449
13 0.8450980 1.681241
14 0.6989700 1.748188
15 1.6720979 0.903090
16 1.4623980 1.863323
17 1.0000000 1.799341
18 1.1139434 1.380211
19 1.5563025 1.982271
20 0.9542425 0.698970
21 0.7781513 1.633468
22 1.0413927 1.707570
23 1.5797836 1.792392
24 1.6901961 1.556303
25 1.6989700 1.806180

Updated on: 10-Nov-2021

93 Views