0

I'm working with log10() and struggle with transforming my dataset so that it works.

My dataset looks somewhat like this:

question_ID estimate
1   1   15000088
2   1   2,00E+07
3   1   1.2e+07
4   2   4.3e+07
5   2   6,00E+08
6   2   14500000
7   3   3500000
8   3   8,00E+06
9   3   1.2e+07

I want to run this syntax:

m <- dt %>% group_by(question_ID) %>% summarize(m=median(log10(estimate)))

and get the error:

Error in Math.factor(estimate) : ‘log10’ not meaningful for factors

I checked with sapply(dt, class) and know that estimate is a factor. I think the problem is that some values are written with , instead of .. Because log10() works with e.g. 8.5E+07 and 8.5e+07 but not 8,5E+07.

How can I replace the decimal character?

I tried several things, but they all don't work.

dt$estimate <- gsub("\\.", ",", dt$estimate)
dt$estimate <- as.factor(dt$estimate)

format(dt, decimal.mark=".")
options(scipen = 999)
Julia
  • 111
  • 3
  • 7
    Try `dt$estimate <- as.numeric(gsub(",", ".", dt$estimate))` – Allan Cameron Mar 06 '20 at 12:58
  • 1
    Does this answer your question? [How to convert a factor to integer\numeric without loss of information?](https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information) – Dave2e Mar 06 '20 at 13:32

0 Answers0