2

I am trying to import a csv dataset which is about the number of benefit recipients per month and district. The table looks like this:

enter image description here

There are 43 variables (months) and 88 observations (districts).

Unfortunately, when I import the dataset with the following code:

D=read.csv2(file="D.csv", header=TRUE, sep=";", dec=".")

all my numbers get converted to characters.

I tried the as.is=T argument, and to use read.delim, as suggested by Sam in this post: Imported a csv-dataset to R but the values becomes factors but it did not help.

I also tried deleting the first two columns in the original csv file to get rid of the district names (which is the only real non-numeric column) but I stil get characters in the imported data frame. Can you please help how I could retain my numerics?

Community
  • 1
  • 1
babesz
  • 43
  • 1
  • 9
  • 1
    Are thousands being imported with comma's? if so they will be read as characters. try grep(",", df$ColumnName) and see if grep gets any results. Also it is helpful if you provide a "reproducible example" so that others can recreate your problem. Even submitting part of the data frame using dput(head(df)) would be useful. – Jonno Bourne Sep 08 '16 at 11:15
  • 1
    Yes it is because of comma's. did you try dec="," ? – ali srn Sep 08 '16 at 11:16
  • Possible duplicate of [Imported a csv-dataset to R but the values becomes factors](http://stackoverflow.com/questions/5187745/imported-a-csv-dataset-to-r-but-the-values-becomes-factors) – agenis Sep 08 '16 at 11:26
  • 1
    Best may be to open csv file in Excel (or Libreoffice spreadsheet) and remove ',' from number formatting (uncheck Thousands separator). – rnso Sep 08 '16 at 11:44
  • if you can't get it to read it as numeric you could convert them after importing using something like: `D[, -1] <- apply(D[, -1], 2, function(x) as.numeric(gsub(",", "", x)))` – eminik Sep 08 '16 at 13:44
  • Yes, thousands were imported with commas -- thank you for the hint. The dec="," argument did work in a sense that it retained numerics, however, it also added three zeros to the end of any number below 1000. The initial problem was that I transformed numeric values to characters when I copied columns from earlier xls sheets to a csv file by using the XLConnect package. I had to go back and specify colTypes in that code. After that numerics have been nicely retained and the commas disappeared too! – babesz Sep 08 '16 at 15:26

0 Answers0