readr::read_csv issue: Chinese Character becomes messy codes

前端 未结 1 1328
天命终不由人
天命终不由人 2021-01-14 15:00

I\'m trying to import a dataset to RStudio, however I am stuck with Chinese characters, as they become messy codes. Here is the code:

library(tidyverse)
df          


        
1条回答
  •  遥遥无期
    2021-01-14 15:07

    This is because that the characters are marked as UTF-8 whereas the actual encoding is the system default (you can get by stringi::stri_enc_get()).

    So, you can do either:

    1) Read data with the correct encoding:

    df <- read_csv("中文,英文\n英文,德文", locale = locale(encoding = stringi::stri_enc_get()))
    

    2) Read data with the incorrect encoding and mark them with the correct encoding later (note that this does not always work):

    df <- read_csv("中文,英文\n英文,德文")
    df <- dplyr::mutate_all(df, `Encoding<-`, value = "unknown")
    

    0 讨论(0)
提交回复
热议问题