Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 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 Answer

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=''))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...