Search code examples
rencodingreadr

R - Special characters reading with read_fwf


I'm trying to convert a large list of strings to dataframe with read_fwf function from readr package and I'm having some troubles with special characters like accents. Could anyone help me with this please? I’m not used to deal with encodings :(

library(readr)
fw <- fwf_widths(c(2,13,2), col_names = c('A','B','C'))
x <- read_fwf('00StackOvérflow00\n',
                      col_positions = fw)

The result is:

A     B                  C    
00    "StackOv\xe9rflow" 00  

As you can see the accent is gone...

Thankss!!

Xevi


Solution

  • You can fix this by changing the encoding via locale to LATIN1:

    library(readr)
    fw <- fwf_widths(c(2,13,2), col_names = c('A','B','C'))
    x <- read_fwf('00StackOvérflow00\n',
                          col_positions = fw, locale = locale(encoding = 'LATIN1'))
    

    Returning:

    # A tibble: 1 x 3
      A     B             C    
      <chr> <chr>         <chr>
    1 00    StackOvérflow 00