Reading Rdata file with different encoding

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

    Another option using dplyr's mutate_if:

    fix_encoding <- function(x) {
      Encoding(x) <- "latin1"
      return(x)
    }
    data <- data %>% 
      mutate_if(is.character,fix_encoding) 
    

    And for factor variables that have to be recoded:

    fix_encoding_factor <- function(x) {
      x <- as.character(x)
      Encoding(x) <- "latin1"
      x <- as.factor(x)
      return(x)
    }
    data <- data %>% 
      mutate_if(is.factor,fix_encoding_factor) 
    

提交回复
热议问题