-2

I have an issue where I have a column in a dataframe that is a character type as there is text in it so I cannot force it to be numeric. But I also have values that show up as scientific notation that I would like to convert and remove the scientific notation.

In the data assume there is an unknown amount of these values that need to be converted. How best can I remove the scientific notation from this type of data?

Example data:

vector <- c("HMM","5.8779396207367296E+17","GOLF","3191963","355534317","8.837971002284400E+16")
exampledf <- as.data.frame(vector)

what I would like it to look like:

vector <- c("HMM","587793962073672960","GOLF","3191963","355534317","80837971002284400")
megmac
  • 547
  • 2
  • 11
  • 1
    In a number like `5.8779E17` how else would you write it without the scientific notation? – Onyambu Apr 12 '23 at 15:26
  • 1
    `as.numeric(vector)` correctly interprets all except HMM/GOLF in your `vector`, so the presence of the scientific notation is not blocking you. If you don't want it printed on the console, see [`?options`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/options.html) for `'scipen'`. – r2evans Apr 12 '23 at 15:28
  • @r2evans I want to retain the text values though and not get rid of those. – megmac Apr 12 '23 at 15:31

1 Answers1

2

You can use type.convert:

as.character(type.convert(as.list(vector), as.is = TRUE))
#[1] "HMM" "587793962073672960" "GOLF" "3191963" "355534317" "88379710022844000" 
Maël
  • 45,206
  • 3
  • 29
  • 67