0

I am working on a dataset that has 2 Price formatted variables that are currently read as character format. I need them to be numerical. I have tried the following different examples but all have created more than a thousand nas when I run it.

dataframe$Price <-as.numeric(dataframe$Price)
dataframe$Price <-as.numeric(as.character(dataframe$Price))

If I run it as

as.numeric(dataframe$Price)

It doesn't change the variable. I am relatively new to R (about 2 months) and I have no idea what I'm doing. I appreciate any help!

Phil
  • 7,287
  • 3
  • 36
  • 66
  • Can you post a reproducible sample of the data using `dput(head(dataframe, 10))`? – norie Oct 18 '21 at 01:34
  • As a note, Rstudio is a text editor for R, often referred to as an "IDE" (integrated development environment). It is not a different programming language from R, but has a symbiotic relationship with R. – Phil Oct 18 '21 at 01:37

1 Answers1

2

If every elements of dataframe$Price is like $12.12, for example,

dummy <- data.frame(
  Price = c("$12.11", "$11.14", "$10.12")
)

   Price
1 $12.11
2 $11.14
3 $10.12

By using stringr::str_replace function,

dummy$Price <- as.numeric(str_replace(dummy$Price, "\\$", ""))

  Price
1 12.11
2 11.14
3 10.12
Park
  • 14,771
  • 6
  • 10
  • 29