Thanks for posting this. I took the liberty of changing your function if you have a data framework with some columns as a 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 a 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 the level names in the "factor" column. Fortunately, I found that this changed all the factors in your data structure to a character (which may not be the best approach, but in my case, this is what I need):
i <- sapply(df, is.factor) df[i] <- lapply(df[i], as.character)
source share