-1

I am reading a csv file into R, while doing this it is converting interger numbers into characters.

I tried to convert it back to numeric as below
as.numeric(factor(x))

but R transformed the numbers I had in positive integers while they were not to begin with.

I tried both adding

stringsAsFactors = F

and

dec="."

when I am importing the data, but also this did not work

  • Please add the R code you are using to read the file. – Tim Biegeleisen Nov 29 '18 at 07:46
  • It is important that we know exactly how you import the data. Otherwise, you're posting a guessing game. So it would be very helpful to give us a [reproducible example](https://stackoverflow.com/help/mcve). – jay.sf Nov 29 '18 at 07:46
  • Possible duplicate of [Importing csv file into R - numeric values read as characters](https://stackoverflow.com/questions/13706188/importing-csv-file-into-r-numeric-values-read-as-characters) – Taher A. Ghaleb Nov 29 '18 at 07:47
  • Check the structure of your data set with `str()`; to change factor to numeric: first change to characters then to numbers: `x – Chris Ruehlemann Nov 29 '18 at 07:48

2 Answers2

1

write

stringsAsFactors = FALSE in your read.csv().

it will work.

Zeeshan
  • 1,160
  • 1
  • 12
  • 25
0

I used

read.csv

instead of

read.table

and then used

x=as.numeric(x)

and it worked