5

我正在尝试进行一些文本处理,并且需要重新编码句子的单词,以便在新变量中以特定方式识别目标单词。例如,给定一个看起来像这样的数据框......

subj <- c("1", "1", "1", "2", "2", "2", "2", "2")
condition <- c("A", "A", "A", "B", "B", "B", "B", "B")
sentence <- c("1", "1", "1", "2", "2", "2", "2", "2")
word <- c("I", "like", "dogs.", "We", "don't", "like", "this", "song.")
d <- data.frame(subj,condition, sentence, word)

 subj condition sentence  word
 1         A        1     I
 1         A        1     like
 1         A        1     dogs.
 2         B        2     We
 2         B        2     don't
 2         B        2     like
 2         B        2     this
 2         B        2     song.

我需要创建一个新列,其中目标单词的每个实例(在此示例中,当 d$word="like" 时)都标记为 0,并且句子块中“like”之前的所有单词和“之后的所有单词”都会递减像”增量。每个主题都有多个句子,句子因条件而异,因此循环需要考虑每个主题、每个句子的目标词实例。最终结果应该是这样的。

 subj condition sentence  word   position
 1         A        1     I        -1
 1         A        1     like      0
 1         A        1     dogs.     1
 2         B        2     We       -2
 2         B        2     don't    -1
 2         B        2     like      0
 2         B        2     this      1
 2         B        2     song.     2

对不起,如果问题措辞不好,我希望它是有道理的!请注意,每个句子中的目标不在同一个位置(相对于句子的开头)。我对 R 很陌生,可以弄清楚如何增加或减少,但不能在每个句子块中同时做这两件事。关于解决此问题的最佳方法有什么建议吗?非常感谢!

4

3 回答 3

5

您可以添加一个索引,然后您可以将其用于相对位置。
使用使其非常容易data.table分解sentence

library(data.table)
DT <- data.table(indx=1:nrow(d), d, key="indx")

DT[, position:=(indx - indx[word=="like"]), by=sentence]

# Results
DT
#    indx subj condition sentence  word position
# 1:    1    1         A        1     I       -1
# 2:    2    1         A        1  like        0
# 3:    3    1         A        1 dogs.        1
# 4:    4    2         B        2    We       -2
# 5:    5    2         B        2 don't       -1
# 6:    6    2         B        2  like        0
# 7:    7    2         B        2  this        1
# 8:    8    2         B        2 song.        2

日期:

如果您有语法错误的句子,您可能想要使用grepl而不是==

DT[, position:=(indx - indx[grepl("like", word)]), by=sentence]
于 2013-03-04T02:10:11.140 回答
4

我认为在文本处理中避免让您的文本条目成为因素是明智的。在这种情况下,我使用了as.character,但我建议设置options(stringsAsFactors=FALSE)

d$position <- with( d, ave(as.character(word), sentence, 
                               FUN=function(x) seq_along(x) - which(x=="like") ) )
> d
  subj condition sentence  word position
1    1         A        1     I       -1
2    1         A        1  like        0
3    1         A        1 dogs.        1
4    2         B        2    We       -2
5    2         B        2 don't       -1
6    2         B        2  like        0
7    2         B        2  this        1
8    2         B        2 song.        2
于 2013-03-04T02:37:06.173 回答
3

常规解决方案plyr

 ddply(d, .(subj, condition, sentence), transform, 
   position = seq_along(word) - which(word == 'like'))
于 2013-03-04T03:33:11.513 回答