Load necessary packages
library(pander)
First create a data frame with ONLY numeric data
#Create vectors with numeric data
vol <- c(67, 86, 91, 109, 135, 48, 118, 124, 135, 124, 124, 96, 90, 114,
148, 73, 93, 105, 163, 141)
bulk <-c(11.5, 13.46, 11.68, 13.57, 19.8, 3.4, 13.69, 21.4, 15.71, 21.4, 16.25,
12.28,17.43, 14.45, 14.28, 9.47, 15.77, 21.57, 21.67, 18.03)
#Create data frame with created vectors
ex.dataframe <- data.frame(Vol = vol, Bulk = bulk)
#output dataframe before log transformation
pander::pander(ex.dataframe)
| Vol | Bulk |
|---|---|
| 67 | 11.5 |
| 86 | 13.46 |
| 91 | 11.68 |
| 109 | 13.57 |
| 135 | 19.8 |
| 48 | 3.4 |
| 118 | 13.69 |
| 124 | 21.4 |
| 135 | 15.71 |
| 124 | 21.4 |
| 124 | 16.25 |
| 96 | 12.28 |
| 90 | 17.43 |
| 114 | 14.45 |
| 148 | 14.28 |
| 73 | 9.47 |
| 93 | 15.77 |
| 105 | 21.57 |
| 163 | 21.67 |
| 141 | 18.03 |
Now we will create a function that takes the log of all numeric elements in a data frame and returns it. To do this we will use a for loop that iterates over each column in a numeric dataframe
dataframe.log <- function(dataframe)
{for(i in 1:ncol(dataframe)){
dataframe.vector <- dataframe[,i]
dataframe.vector <- log(dataframe.vector)
dataframe[,i] <- dataframe.vector
}
return(dataframe)
}
Finally we pass the dataframe we created to the newly created function and output the log transformed values using pander
ex.dataframe <- dataframe.log(ex.dataframe)
#output transformed dataframe
pander::pander(ex.dataframe)
| Vol | Bulk |
|---|---|
| 4.205 | 2.442 |
| 4.454 | 2.6 |
| 4.511 | 2.458 |
| 4.691 | 2.608 |
| 4.905 | 2.986 |
| 3.871 | 1.224 |
| 4.771 | 2.617 |
| 4.82 | 3.063 |
| 4.905 | 2.754 |
| 4.82 | 3.063 |
| 4.82 | 2.788 |
| 4.564 | 2.508 |
| 4.5 | 2.858 |
| 4.736 | 2.671 |
| 4.997 | 2.659 |
| 4.29 | 2.248 |
| 4.533 | 2.758 |
| 4.654 | 3.071 |
| 5.094 | 3.076 |
| 4.949 | 2.892 |