0

I have tables with one or more columns containing unprintable characters as column name as shown below (in the second and fifth column). These columns occur at random positions in the tables. I am not able to locate these columns through R code to rename it by column position. Please advise.

>test <- structure(list(AA = 1:3, X... = c(23L, 55L, 33L), 
                BB = c(10, 20, 30), 
                CC = structure(1:3, .Label = c("a", "b", "c"), 
                class = "factor"), ï.. = c(12L, 34L, 534L)), 
                .Names = c("AA", "ï..", "BB", "CC", "ï.."), 
               class = "data.frame", row.names = c(NA,-3L))
> test
  AA ï.. BB CC ï..
1  1  23 10  a  12
2  2  55 20  b  34
3  3  33 30  c 534
RanonKahn
  • 853
  • 10
  • 34
  • It is better to use `dput` to show the data. Perhaps `i1 <- grepl("![[:ascii:]]+", names(data), perl = TRUE); names(data)[i1] <- replacementvector` – akrun Apr 29 '17 at 12:12
  • A code example will help. Use `dput(head(my_object))` if copy paste doesn't help. – Pierre L Apr 29 '17 at 12:12
  • You can locate with `names(test)[grepl('ï', names(test))]`. I used the first special character to search for. You can customize based on results. – Pierre L Apr 29 '17 at 13:17
  • Try with `grepl("[[:cntrl:]]", stringi::stri_enc_toascii(names(test)))# [1] FALSE TRUE FALSE FALSE TRUE` as showed in the dupe link – akrun Apr 29 '17 at 13:44
  • @akrun, Thanks. I somehow missed the earlier entry on "detecting non ascii characters in a string" in this forum. Should I delete this question? – RanonKahn Apr 29 '17 at 13:56
  • @PierreLafortune, the challenge is the special character is not always the same. I missed to mention this in my query. – RanonKahn Apr 29 '17 at 13:58
  • No, you don't have to delete it. In fact, dupe questions makes it easier to search on google – akrun Apr 29 '17 at 14:01

0 Answers0