2013-10-21 20 views
5

想知道,是否有改變R列表對象的字符編碼? 我有一種感覺,我將不得不採取多個步驟,但我不確定。 我已經搜索了這個主題,但我沒有從互聯網上獲得幫助。R:如何更改列表對象的字符編碼

例如,考慮以下幾點:

library(twitteR) 
library(RJSONIO) 


#Authorize with Twitter's API 
reqURL <- "https://api.twitter.com/oauth/request_token" 
accessURL <- "http://api.twitter.com/oauth/access_token" 
authURL <- "http://api.twitter.com/oauth/authorize" 
consumerKey = "myconsumerkey" 
consumerSecret = "myconsumersecret" 
twitCred <- OAuthFactory$new(consumerKey=consumerKey, 
         consumerSecret=consumerSecret, 
         requestURL=reqURL, 
         accessURL=accessURL, 
         authURL=authURL) 
    twitCred$handshake() 

    B<-read.csv("BCorp RAW.csv") 
    handles<-B$Twitter.handle 
    handles<-na.omit(handles) 

    start <- getUser(handles[12])       

    library(rjson) 
    friends.object<- lookupUsers(start$getFriendIDs(), includeNA=TRUE) 
    followers.object<-lookupUsers(start$getFollowerIDs(), includeNA=TRUE) 

命令

followers.object<-lookupUsers(start$getFollowerIDs(), includeNA=TRUE) 

引發以下錯誤:

Error in twFromJSON(out) : 
    Error: Malformed response from server, was not JSON. 
    The most likely cause of this error is Twitter returning a character which 
    can't be properly parsed by R. Generally the only remedy is to wait long 
    enough for the offending character to disappear from searches (e.g. if 
    using searchTwitter()). 

我怎麼能允許R保存在特殊字符與微博合作? 希望我編輯好聽......讓我知道如果你要我來編輯它再次

謝謝你,

+1

你能否通過提供一個[可重現的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)來詳細說明你的問題,並展示你期望什麼? –

+0

不需要。你需要想出一個更好的例子,它首先顯示*你如何將數據讀入R,因爲R默認將非ASCII字符轉換爲UTF-8(或者也許是「未知」 )。例如,檢查'Encoding(「£」)',它給出了'[1]「UTF-8」'。 –

+0

好嗎?我無法真正添加真實數據,因爲我的原始數據位於.csv文件中 –

回答

0

有一個Encoding<-功能,您可以指定字符向量的編碼。列表元素可能在同一列表中具有不同的編碼,但字符向量中的所有元素都將具有相同的編碼。此外,還有'fileEncoding'和'encoding'參數給read.table及其read.*表兄弟,允許你讀取除默認值以外的字符數據。

?Encoding 
?read.table 

還有很多在Rhelp檔案中回答的編碼問題。