Reading Rdata file with different encoding

后端 未结 4 1075
别跟我提以往
别跟我提以往 2020-12-10 04:50

I have an .RData file to read on my Linux (UTF-8) machine, but I know the file is in Latin1 because I\'ve created them myself on Windows. Unfortunately, I don\'t have access

4条回答
  •  一向
    一向 (楼主)
    2020-12-10 05:34

    following up on previous answers, this is a minor update which makes it work on factors and dplyr's tibble. Thanks for inspiration.

    fix.encoding <- function(df, originalEncoding = "UTF-8") {
    numCols <- ncol(df)
    df <- data.frame(df)
    for (col in 1:numCols)
    {
            if(class(df[, col]) == "character"){
                    Encoding(df[, col]) <- originalEncoding
            }
    
            if(class(df[, col]) == "factor"){
                            Encoding(levels(df[, col])) <- originalEncoding
    }
    }
    return(as_data_frame(df))
    }
    

提交回复
热议问题