How to convert .rds files to .csv in R without losing special characters?

Viewed 7521

Good day! I need some help for this. So I am trying to do some text mining where I am counting the word frequencies for every word in a text. I was able to do it fine in R with all the different characters, but the problem lies when I export it to .csv file. Basically, I am working on Hungarian text and when I try to save my data frame to .csv, three accented letters (ő, ű, ú) get converted to non-accented ones (o, u and u). It doesn't happen when the file is in .rds but I need to convert it to a .csv file so one of my consultants (zero knowledge of programming) can look at it in a normal Excel file. I tried some tricks e.g. making sure Notepad++ is in UTF-8 format, adding a line like this (fileEncoding = "UTF-8" or encoding ="UTF-8") when writing the .csv file using the write.csv command, but it doesn't work.

Hope you can help me.

Thank you.

2 Answers

write.csv() works with the three characters you mentioned in the question.

Example

First create a data.frame containing special characters

library(tidyverse)

# Create an RDS file
test_df <- "ő, ű, ú" %>% 
  as.data.frame

test_df
#         .
# 1 ő, ű, ú

Save it as an RDS

saveRDS(test_df, "test.RDS")

Now read in the RDS, save as csv, and read it back in:

# Read in the RDS
df_with_special_characters <- readRDS("test.RDS")

write.csv(df_with_special_characters, "first.csv", row.names=FALSE)
first <- read.csv("first.csv")
first
#         .
# 1 ő, ű, ú

We can see above that the special characters are still there!

Extra note

If you have even rarer special characters, you could try setting the file encoding, like so:

write.csv(df_with_special_characters, "second.csv", fileEncoding = "UTF-8", row.names=FALSE)
second <- read.csv("second.csv")
# second
# #         .
# # 1 ő, ű, ú

With the writexl package you could use write_xlsx(...) to write an xlsx file instead. It should handle unicode just fine.

Related