-2

First off, I have looked at all the examples and previous questions and have not been able to find a usable answer to my situation.

I have a data set of 300ish independent variables I'm trying to bring into R. The variables are all classified as factors. In my csv file I'm uploading, all of the variables are pricing data with two decimal places. I have used the following code and some of the variables have been converted with decimals. However, many of the converted columns are filled with NAs; in fact, some entire columns are completely NAs.

dsl$price = as.numeric(as.factor(dsl$price))    # <- this completely changes the data into something unrecognizablbe
dsl$price = as.numeric(as.character(dsl$price)) # <- lots of NAs or totally NAs

I've tried to recode the variables in the original CSV file to numeric, but with no luck.

Frank
  • 66,179
  • 8
  • 96
  • 180
  • 2
    Don't read-in strings as factors, just keep them as character: `read.csv( ... , stringsAsFactors = FALSE)` – DanY Aug 15 '18 at 21:03
  • You need as.character in there: `dsl$price <- as.numeric(as.character(as.factor(dsl$price)))` – DanY Aug 15 '18 at 21:03
  • Thanks, Dan. This appears to work on about half of the variables. It doesn't seem to matter if I do it one column at a time. – Michael Westerman Aug 15 '18 at 21:29
  • Upon further inspection, the variables coming in as NA after doing your suggestion are the negative values. – Michael Westerman Aug 15 '18 at 21:46
  • Problem solved. Ctl+1 to recode a variable in excel will not actually do that. R will read in the variable as a character. If you change the type in the "number" section of Excel, it will change it to a value R can understand. – Michael Westerman Aug 15 '18 at 22:18

1 Answers1

1

Convert the factor into character which can then be converted into numeric

dsl$price <- as.numeric(as.character(dsl$price))
SmitM
  • 1,366
  • 1
  • 8
  • 14