1

I have a dataframe named COMP with 24 columns in it. I would like to create another dataframe named COMPLog with log10 versions of all 24 columns/variables in it.

COMPLog <- lapply(COMP[,c(1:24)], function(x) log(x))

Error in log(x) : non-numeric argument to mathematical function

I've trawled through other questions and tried using df.log <- log(df) from one of the earlier questions, but I am unsure how to modify it for my situation. I've also tried mutate and transmute but I did not manage to get my code to work.

What code should I use to apply log10 to all of the variables in a dataframe? (and then to save it as another dataframe).

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Emerlyn Ng
  • 11
  • 4
  • What is wrong with just using `log(COMP)` ? By the way, the error says you have got some factor or characters inside your data. You can find which column by `sapply(COMP,class)`. – maydin Feb 05 '21 at 06:02
  • 1
    You can use `df %>% dplyr::mutate(across(is.numeric, log))` for this. But if you give us a sample of your data it will be easy for us to solve. Look [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for giving a reproducible example – Mohan Govindasamy Feb 05 '21 at 06:05
  • @maydin thanks, I figured it out! One of my columns contained characters because it contained information about the conditions my variables were in. I tried both solutions log(COMP) and the dplyr solution and they worked perfectly! – Emerlyn Ng Feb 05 '21 at 06:11
  • @MohanGovindasamy thanks for the link! I'm a first time user of Stack and I will make sure to give a reproducible example next time. – Emerlyn Ng Feb 05 '21 at 06:12

0 Answers0