0

I downloaded historical prices of an index but all prices are characters, that is are of the form : "24,31" (and i checked the mode).

I tried several things, such as :

as.numeric(as.character(VDAXcsv$Dernier))

which returns only NAs, or :

sapply(VDAXcsv$Dernier, as.numeric) | sapply(VDAXcsv, as.numeric)

Or simply

as.numeric(VDAXcsv)

And still only NAs, besides I tried to put "stringsAsFactors=FALSE" into my "read.zoo" function but it doesn't change anything. as.format doesn't work either.

ThomasFR
  • 7
  • 3
  • Avoid the problem if possible, import the data with proper decimal separator, see: https://stackoverflow.com/q/6123378/680068 – zx8754 Nov 23 '21 at 10:09

2 Answers2

1

You can use this function:

library(readr)

parse_number("24,31", locale = locale(decimal_mark = ","))

Is vectorized, so just put VDAXcsv$Dernier as first argument.

gss
  • 1,334
  • 6
  • 11
  • 1
    be careful though with parse_number as `parse_number("2g4,31", locale = locale(decimal_mark = ","))` would give you a 2 as outcome for example, in many scenario's one would rather have a NA returned. – Merijn van Tilborg Nov 23 '21 at 08:50
  • Well I tried it, and I finally get numeric values for my prices, but my index of dates is now returned as NAs.. How should I handle this issue ? EDIT : That is because i added this to my line of code : names(VDAXnumeric) <- "VDAX.d" so nevermind – ThomasFR Nov 23 '21 at 08:55
1
x <- "24,31"
y <- as.numeric(gsub(",", ".", x))
y
# [1] 24.31
class(y)
# [1] "numeric"

A side note

I think depending on the data file you have, you might even want to prevent this to happen in the first place defining dec. Be careful if your sep is a comma as well though. It still can be an option, so you do not get your values as a character and therefor no need to do any replacements.

fread(file, header = T, sep = ";", dec = ",") # fread is data.table, but I think read.csv and any others support it as well
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22