Reading Rdata file with different encoding

后端 未结 4 1077
别跟我提以往
别跟我提以往 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:23

    Thank you for posting this. I took the liberty to modify your function in case you have a dataframe with some columns as character and some as non-character. Otherwise, an error occurs:

    > fix.encoding(adress)
    Error in `Encoding<-`(`*tmp*`, value = "latin1") :
     a character vector argument expected
    

    So here is the modified function:

    fix.encoding <- function(df, originalEncoding = "latin1") {
        numCols <- ncol(df)
        for (col in 1:numCols)
                if(class(df[, col]) == "character"){
                        Encoding(df[, col]) <- originalEncoding
                }
        return(df)
    }
    

    However, this will not change the encoding of level's names in a "factor" column. Luckily, I found this to change all factors in your dataframe to character (which may be not the best approach, but in my case that's what I needed):

    i <- sapply(df, is.factor)
    df[i] <- lapply(df[i], as.character)
    

提交回复
热议问题