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
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)