0

I have a Spanish computer and am working in a French language environment, where the decimal separator is usually a comma. I am more used to working with periods as decimal separators, so changed the separator in Microsoft Excel and also specified the separator when reading in the file to R as below:

eg <- read.csv("D:/02_COVID-19/Analysis/eglonglat.csv", dec = ".", sep = ",", header = TRUE, stringsAsFactors = FALSE)

However, it seems that the comma separator is being ignored, because the .csv has three columns and when read into R it has only one column.

I´m using a clean install of R 4.0 and RStudio 1.3.959 on a Windows 7 laptop.

Any ideas? I was only trying to copy some example data from a stack overflow post that used periods as decimal separators - not sure whether it is the intervention of excel that is causing the problem here (it looks like the decimal separators are periods but maybe they are not in the background?)

Here is what the output looks like in R after being read in:

> eg
            index.lat.lon
1    1;51.513393;-0.11565
2   2;51.513428;-0.115461
3   3;51.513428;-0.115462
4   4;51.513428;-0.115465
5    5;51.513428;-0.11547
6   6;51.513432;-0.115462
7   7;51.513432;-0.115467
8   8;51.513435;-0.115471
9   9;51.513439;-0.115468
10 10;51.513439;-0.115469

Amy M
  • 967
  • 1
  • 9
  • 19

1 Answers1

1

In read.csv, you need sep = ";" to separate columns and dec = "," to consider decimals with comma separator.

eg <- read.csv("D:/02_COVID-19/Analysis/eglonglat.csv", sep = ";", dec = ",")

These are default settings in read.csv2

eg <- read.csv2("D:/02_COVID-19/Analysis/eglonglat.csv")

Since you are on R 4.0.0, you don't need stringsAsFactors = FALSE.


Debugging further it seems OP has "." as decimal separator so this works :

eg <- read.csv("D:/02_COVID-19/Analysis/eglonglat.csv", sep = ";")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks for the tip re `stringsAsFactors` good to know it is no longer converting to factor as the default... – Amy M Jun 05 '20 at 10:01
  • @AmyM did the answer work? Looking at the output now i think you might need `eg <- read.csv("D:/02_COVID-19/Analysis/eglonglat.csv", sep = ";")` – Ronak Shah Jun 05 '20 at 10:04
  • They are now read in as three columns but the col class is character - wouldn´t this normally detect they are numeric? – Amy M Jun 05 '20 at 10:04
  • you are right - not specifying the decimal separator seems to work (they are interpreted as numeric with the decimal separator in the right place) – Amy M Jun 05 '20 at 10:08
  • Thanks to @akrun for directing to this [question](https://stackoverflow.com/questions/6123378/how-to-read-in-numbers-with-a-comma-as-decimal-separator) which explains that in countries that use the comma as a decimal separator, the .csv file column separator is actually a semi-colon, not a comma. So even though I changed the decimal separator commas to periods, the column separator was still a semi-colon, which is handled by specifying `sep = ";"`with `read.csv`or using `read.csv2` instead where the semi-colon is the default column separator. – Amy M Jun 06 '20 at 09:23