【发布时间】:2012-08-09 09:07:06
【问题描述】:
我有一个数据集,其中包含不适当间隔的句子。我正在尝试想出一种方法来删除一些空格。
我从一个句子开始,然后将其转换为单词的数据框:
> word5 <- "hotter the doghou se would be bec ause the co lor was diffe rent"
> abc1 <- data.frame(filler1 = 1,words1=factor(unlist(strsplit(word5, split=" "))))
> abc1
filler1 words1
1 1 hotter
2 1 the
3 1 doghou
4 1 se
5 1 would
6 1 be
7 1 bec
8 1 ause
9 1 the
10 1 co
11 1 lor
12 1 was
13 1 diffe
14 1 rent
接下来我使用下面的代码来尝试拼写检查和组合之前或之后的单词组合的单词:
abc2 <- abc1
i <- 1
while(i < nrow(abc1)){
print(abc2)
if(nrow(aspell(abc1$words1[i])) == 0){
print(paste(i,"Words OK",sep=" | "));flush.console()
i <- i + 1
}
else{
if(nrow(aspell(abc1$words1[i])) > 0 & i != 1){
preWord1 <- abc1$words1[i-1]
postWord1 <- abc1$words1[i+1]
badWord1 <- abc1$words1[i]
newWord1 <- factor(paste(preWord1,badWord1,sep=""))
newWord2 <- factor(paste(badWord1,postWord1,sep=""))
if(nrow(aspell(newWord1)) == 0 & nrow(aspell(newWord2)) != 0){
abc2[i,"words1"] <-as.character(newWord1)
abc2 <- abc2[-c(i+1),]
print(paste(i,"word1",sep=" | "));flush.console()
i <- i + 1
}
if(nrow(aspell(newWord1)) != 0 & nrow(aspell(newWord2)) == 0){
abc2[i ,"words1"] <-as.character(newWord2)
abc2 <- abc2[-c(i-1),]
print(paste(i,"word2",sep=" | "));flush.console()
i <- i + 1
}
}
}
}
玩了一段时间后,我得出的结论是我需要某种类型的迭代器,但不确定如何在 R 中实现它。有什么建议吗?
【问题讨论】:
-
你能告诉我们这怎么行不通吗?我认为您可能正在寻找 sapply 或 lapply 功能。如果您定义自己的函数,然后执行
lapply(abc1$words1, yourFunctionNameHere),它将遍历adc1$words1的每个元素,并使用作为参数传递的该元素调用您的函数。如果还有其他参数要传递给函数,您可以在函数名之后传递这些参数
标签: r loops iterator spell-checking plyr