Weird chðracŁer

Mærklige Łegn
Author

Søren O’Neill & Steen Harsted

Published

September 29, 2026


There are typically about one hundred keys on a laptop keyboard. Laptops sold in Denmark typically include the ‘Æ’, ‘Ø’ and ‘Å’ keys. Laptops and keyboards sold in England typically do not.

What about laptops and keyboards sold in China and Korea?

The point is this: Computers need to handle a lot more than 28 different characters. To do this effectively, computers need to know how characters are encoded in a text file.

The original text encoding was ASCII, which allowed for just 256 different characters. Since then, UTF has taken over and can encode more than a million different characters.

Here is the problem:

  • Computers need to know how a text file is encoded.
  • Microsoft (Excel in particular) will default to Latin 1 (ISO-8859-1 or ISO-8859-15) encoding.

Microsoft Excel is perfectly able export to UTF8 and R is perfectly able to import Latin 1 – but we must avoid getting them crossed.

If e.g. a text string like ‘Søren’ turns up in your data as ‘Søren’ or ‘S�ren’ that typically indicates there is a mismatch between the expected and actual character encoding.

Here is the solution:

  • Avoid using Excel, or
  • Make sure Excel is exported as UTF8, or
  • Make sure R imports as Latin1

In Excel, the option to choose character encoding is associated with the export to CSV functionality.

In R, this could look like:

data <- read_csv2(filename, locale=readr::locale(encoding = "Latin1"))

Der er typisk omkring et hundrede taster på et laptop-tastatur. Laptops, der sælges i Danmark, har typisk tasterne ‘Æ’, ‘Ø’ og ‘Å’. Laptops og tastaturer, der sælges i England, har typisk ikke disse taster.

Hvad med laptops og tastaturer, der sælges i Kina og Korea?

Pointen er denne: Computere skal kunne håndtere langt mere end 28 forskellige tegn. For at kunne gøre dette effektivt skal computere vide, hvordan tegn er kodet i en tekstfil.

Den oprindelige tekstkodning var ASCII, som kun kunne håndtere 256 forskellige tegn. Siden da har UTF taget over og kan kode mere end en million forskellige tegn.

Her er problemet:

  • Computere skal vide, hvordan en tekstfil er kodet.
  • Microsoft (Excel i særdeleshed) vil som standard bruge Latin 1 (ISO-8859-1 eller ISO-8859-15).

Microsoft Excel kan sagtens eksportere til UTF8, og R kan sagtens importere Latin 1 – men vi skal undgå at få dem blandet sammen.

Hvis f.eks. en tekststreng som ‘Søren’ optræder i dine data som ‘Søren’ eller ‘S�ren’, er det typisk tegn på, at der er et mismatch mellem den forventede og den faktiske tegnkodning.

Her er løsningen:

  • Undgå at bruge Excel, eller
  • Sørg for, at Excel eksporterer som UTF8, eller
  • Sørg for, at R importerer som Latin1

I Excel sættes tegnkodningen ifm eksport til CSV funktionen.

I R kunne det se sådan ud:

data <- read_csv2(filename, locale=readr::locale(encoding = "Latin1"))