How to import a file . xls in R keeping accented names in categorical variables?

Asked

Viewed 259 times

1

I imported a file into . xls that contains columns with drug names and their class. Some names have accents like "Antipsychotic", "Benzodiazepine" and "Tricyclic Antidepressant". I used read.xlsx as below:

require(xlsx)
AskStack <- read.xlsx("file.xlsx",sheetIndex = 1)

And when I checked the date.frame I saw that the names were with encoding modifications as you see ahead:

> dput(AskStack)
structure(list(Medic.1 = structure(c(24L, 13L, 15L, 3L, 32L, 
9L, 2L, 17L, 15L, 21L, 25L, 32L, 17L, 21L, 9L, 9L, 15L, 15L, 
26L, 31L, 31L, 31L, 20L, 31L, 21L, 24L, 21L, 31L, 24L, 17L, 2L, 
32L, 31L, 23L, 26L, 25L, 31L, 11L, 31L, 12L), .Label = c("alprazolam", 
"Alprazolam", "Amitriptilina", "Bupropiona", "citalopram", "clomipramina", 
"clonazepam", "Clonazepam", "Desvenlafaxina", "Diazepam", "Donarem", 
"Donaren", "Duloxetina", "escitalopram", "Escitalopram", "fluoxetina", 
"Fluoxetina", "fluvoxamina", "imipramina", "Imipramina", "Olanzapina", 
"paroxetina", "Pondera", "Quetiapina", "Quetros", "Risperidona", 
"Rivotril", "Sem uso informado", "sertralina", "Trazodona", "Venlafaxina", 
"Venlaxin"), class = "factor"), classe = structure(c(2L, 4L, 
4L, 1L, 5L, 5L, 3L, 4L, 4L, 2L, 2L, 5L, 4L, 2L, 5L, 5L, 4L, 4L, 
2L, 5L, 5L, 5L, 1L, 5L, 2L, 2L, 2L, 5L, 2L, 4L, 3L, 5L, 5L, 4L, 
2L, 2L, 5L, 4L, 5L, 4L), .Label = c("Antidepressivo Tricíclico", 
"Antipsicótico", "Benzodiazepínico", "ISRS", "ISRSN", "não se aplica"
), class = "factor")), row.names = c(NA, 40L), class = "data.frame")

How do I import without having to change manually after import?

  • 3

    Try to specify the enconding in function encoding = "UTF-8". The problem must be out there.

1 answer

1


You can use the package readxl, that solves the problem automatically:

> library(readxl)
> read_xlsx("Drogas.xlsx")
# A tibble: 3 x 1
  Drogas                   
  <chr>                    
1 Antipsicótico            
2 Benzodiazepínico         
3 Antidepressivo Tricíclico

I particularly prefer the readxl to read excel files in R.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.