1

here is my function defintion

get_df <- function(c_code) {
  df %>%
    filter(
    country_code == c_code
      )
}

following is my sample dataframe

x<- c("abc", "USA", "ghi", "jkl")
y<- c("bnj", "GBR", "sfd", "lai")
df <- data.frame(rbind(x, y))
colnames(df) <- c("p", "country_code", "q", "r")

using lapply("USA", get_df) gives following output with ÿþ encoding

resultant data frame with garbled data values

  • 1
    interesting but can't reproduce. – s_baldur Jan 22 '19 at 11:46
  • What if you use just base `R`: `get_df <- function(c_code) df[df[["country_code"]] == c_code, ]` – s_baldur Jan 22 '19 at 11:48
  • 2
    I can't reproduce your issue either, based on the sample data you give. But it seems that [ÿþ is the byte order mark in UTF-16](https://stackoverflow.com/questions/26493053/remove-%C3%BF%C3%BE-from-string). If you're reading in some dataset, perhaps you should try reading your data in UTF-8 by using e.g. `read.table(..., encoding = "UTF-8")`. – Maurits Evers Jan 22 '19 at 12:04
  • Hi Maurits. It looks like I get this issue when I use Markdown. The output is ok with this sample data when I am using the RStudio console. – Ravi Shankar Hela Jan 22 '19 at 12:32
  • @RaviShankarHela You should be able to set the text encoding when `render`ing an RMarkdown document. Take a look at [this post](https://stackoverflow.com/questions/47402718/rmarkdown-utf-8-works-with-knit-button-but-not-with-render). – Maurits Evers Jan 22 '19 at 13:05

0 Answers0