2014-03-29 58 views
0

我的數據框出現問題。我正在嘗試根據不同的輸入評分推文。 除了最後一部分,一切都很順利..我無法弄清楚什麼是錯的。 我的數據框由鳴叫組成。數據幀中的grep粘貼錯誤

我投入了大量的度假勝地爲載體,使用grep(膏($ COUNTRY $,崩潰=「|」)來搜索它 但是最後一個給了我這個錯誤:

Error in $<-.data.frame (*tmp* , "score", value = 10) : replacement has 1 row, data has 0

這是我的代碼:

italiaans <- c("Rome", "venice", "Venezia", "Roma", "Florence", "Firenze","milan", "Città Metropolitana di Milano", "Milano", "naples", "napoli", "Bologna", "riccione", "bellaria-igea Marina","Selva Di Val Gardena", "Wolkenstein in Gröden", "Cattolica") 
portugees <- c("coimbra", "azores", "aveiro", "evora" ,"porto", "madeira", "sintra", "obidos", "algarve", "lisbon","portugal", "brasilie", "brasil","Rio De Janeiro", "rio", "sao Paulo", "salvador", "florianopolis", "foz do iguacu", "buzios", "manaus", "jericoacoara", "curitiba", "gramado") 
piete <- c("BuenosAires") 
    totaltweetsScore[grep(paste(france, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 1 
    totaltweetsScore[grep(paste(turkey, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 2 
    totaltweetsScore[grep(paste(germany, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 3 
    totaltweetsScore[grep(paste(portugees, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 4 
    totaltweetsScore[grep(paste(Spain, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 5 
    totaltweetsScore[grep(paste(us, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 6 
    totaltweetsScore[grep('Holland', totaltweetsScore$text, ignore.case=TRUE),]$score <- 7 
    totaltweetsScore[grep(paste(italiaans, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 8 
    totaltweetsScore[grep(paste(maleisie, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 9 
    totaltweetsScore[grep(paste(piete, collapse="|"), totaltweetsScore$text, ignore.case=TRUE),]$score <- 10 
    totaltweetsScore[grep('NA', totaltweetsScore$text, ignore.case=TRUE),]$score <- NA 

正如你所看到的一切都只是複製粘貼...除了要使用的矢量的名稱和添加的號碼..

哪有我修復這個錯誤? 如果我把其他載體,然後Piete,像italiaans,它的作品。

回答

0

因爲我們沒有totaltweetsScore很難重現你的錯誤,但嘗試改變您的搜索/從grep]$score <-取代一般圖案:

totaltweetsScore[grepl(paste(piete, collapse="|"), totaltweetsScore$text, ignore.case=TRUE), "score"] <- 10 

,看看是否有差別。我懷疑BuenosAires不在totaltweetsScore

+0

奇怪的是,它確實解決了我的問題。 我不知道它爲什麼這樣做......但嘿它的作品! – brian

+0

查看'?grep',看看'grep'和'grepl'返回值是什麼。這可能會給你一個提示(但我會很高興解釋你是否想要澄清)。 – hrbrmstr

+0

似乎對我來說,當某些東西匹配時,grepl也會返回..而當grep沒有匹配時grep會給出錯誤。 – brian