R version 4.2.0 and Swedish letters (ä ö å) not working in newest R release. Anyone found a solution?

Viewed 261

I have updated to the latest R release (R version 4.2.0), but I am now facing the problem that all the Swedish special letters cannot be read anymore. I am working with a database that has many Swedish letters in its factor lables and even if I am reading them in as strings R doesn't recognise them, with the consequence that all summary tables that are based on these factors as groups are not calculated correctly anymore. The code has been working fine under the previous release (but I had issues with knitting Rmarkdown files, therefore the need for updating). I have set the encoding to iso-5889-4 (which is nothern languages) after UTF-8 has not worked. Is there anything else I could try? Or has anyone come to a solution on how to fix this, other than to rename all lables before reading in the .csv files? (I would really like to avoid this fix, since I am often working with similar data)

I have used read.csv() and it produces cryptic outputs replacing the special letters with for example <d6> instead of ö and <c4> instead of ä. I hope that someone has an idea for a fix. Thanks.

edit: I use windows.

Sys.getlocale("LC_CTYPE") [1] "Swedish_Sweden.utf8"

2 Answers

Use the encoding parameter

I have been able to detect failed loads by attempting to apply toupper to strings, which gives me errors such as

Error in toupper(dataset$column) : 
invalid multibyte string 999751

This is resolved and expected outcomes obtained by using

read.csv(..., encoding = 'latin1')
or
data.table::fread(..., encoding = 'Latin-1')

I believe this solution should apply to Swedish characters as they are also covered by the Latin-1 encoding.

I have the same problem, what worked for me was like the answer above said but I used encoding ISO-8859-1 instead. It works for both reading from file and saving to file for Swedish characters å,ä,ö,Å,Ä,Ä, i.e:

read.csv("~/test.csv", fileEncoding = "ISO-8859-1")

and

write.csv2(x, file="test.csv", row.names = FALSE, na = "", fileEncoding = "ISO-8859-1")

It's tedious but it works right now. Another tip is if you use Rstudio is to go to Global options -> Code -> Saving and set your default text encoding to ISO-8859-1 and restart Rstudio. It will save and read your scripts in that encoding as default if I understand correctly. I had the problem when I opened my scripts with Swedish characters, they would display wrong characters. This solution fixed that.

Related