Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

r - Removing unicode symbols from column names

I am trying to grab some statistics from the fifa.com by using XML package. The import is successful but the column names have unicode symbols. I want to remove those symbols.

This is how I have got the data,

library(XML)
url <- "http://www.fifa.com/worldcup/statistics/teams/disciplinary.html"
foulbycountry <- readHTMLTable(url)
foulbycountry1 <- do.call(rbind.data.frame, foulbycountry)

The variable names include two characters that I want to remove. I have tried to create a new object but it is not working. For example,

country <- foulbycountry1$Teams??
fouls.committed <- foulbycountry1$Fouls Committed??

which gives me the following output,

> country <- foulbycountry1$Teams??
Error: unexpected input in "country <- foulbycountry1$Teams?"
> fouls.committed <- foulbycountry1$Fouls Committed??
Error: unexpected symbol in "fouls.committed <- foulbycountry1$Fouls Committed"

Is there any way you can suggest so that I can remove those extra unicode characters?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

iconv is one option ...

names(foulbycountry1) <- iconv(names(foulbycountry1), to='ASCII', sub='')
names(foulbycountry1)
# [1] "Teams"                           "Teams"                           "Matches Played"                 
# [4] "Yellow Card"                     "Second yellow card and red card" "Red Cards"                      
# [7] "Fouls Committed"                 "Fouls Suffered
"              "Fouls causing a penalty"    

This will remove any non-ASCII characters. One of the columns has linebreaks at the end of it. To remove these, too, you can use

gsub('
|
', '', iconv(names(foulbycountry1), to='ASCII', sub=''))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...