2014-04-01 21 views
0

我嘗試,直到滿足條件重複中的R兩個環路,直到condtion是R中滿足

第一個循環是從字符串刪除關鍵字(pattern)的第一實例中重複兩個循環多次。 第二個循環是計算同一個字符串中關鍵字實例的數量。

的數據是在與3列的數據幀 - 串,關鍵字和次關鍵字的數目在串被重複(NoKW)

string <- c (" temple san temple lush ", " mohito sudoku war ", " martyr martyr metal martyr", " jump statement statement ", " window capsule turn ") 
keyword <- c (" temple ", " sudoku ", " martyr " , " statement ", " capsule ") 
NoKW <- c(2,1,3,2,1) 
data <- data.frame (string, keyword, NoKW) 
data$string <- as.character(data$string) 
data$keyword <- as.character(data$keyword) 

的想法是順序地刪除關鍵字的情況下,直到我只有一個關鍵字的實例在相應的字符串中。

我試圖重複使用如下。

repeat 
{ 
M <- nrow(data); 
for (j in 1:M){ 
if(1 < data[j,3]) data[j,1] <- str_replace(data[j,1], data[j,2], " ") 
}; 
for (i in 1:M){ 
data[i,3] <- sum(str_count(data[i,1], data[i,2])) 
}; 
max <- as.numeric(max(data$NoKW)); 
if (max = 1) 
break; 
} 

但它提供了以下錯誤

Error: unexpected '=' in: 
" }; 
if (max =" 
>   break; 
Error: no loop for break/next, jumping to top level 
> } 
Error: unexpected '}' in "}" 
> 

我加入R環路,所以你能告訴我在哪裏,我錯了。

+0

不應該是'如果(最大== 1)'? –

+0

我認爲你需要:'if(max == 1)' – Gudgip

+1

首先,你正在比較和分配,所以'=='和'='在最後的'if'語句中。第二:**分號**:只說* no *(這不是javascript :-)。第三,SO R維基http://stackoverflow.com/tags/r/info有很多很好的介紹資源,可以幫助您選擇良好的R實踐(並且更熟悉該語言)。 – hrbrmstr

回答

1

這個想法是按順序刪除關鍵字的實例,直到我有 關鍵字在相應的字符串中只有一個實例。

你並不需要一個for循環:

#split your strings by space 
substrings <- strsplit(string, " ", fixed=TRUE) 

#remove spaces from keywords 
keyword_clean <- gsub("\\s", "", keyword) 

#loop over the strings 
sapply(substrings, function(s) { 
    #which word is duplicated and a keyword 
    rm <- which(duplicated(s, fromLast=TRUE) & s %in% keyword_clean) 
    #remove the duplicated keywords 
    if (length(rm > 0)) s <- s[-rm] 
    #paste words together 
    paste(s, collapse=" ") 
    }) 

#[1] " san temple lush"  " mohito sudoku war" " metal martyr"  " jump statement"  " window capsule turn"