Recoding target variable in R

I am trying to do some word processing and have to transcode sentence words so that the target word is identified in a certain way in a new variable. For example, given a data frame that looks like this ...

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.

I need to create a new column for which each instance of the target word (in this example, when d $ word = "like") is marked with 0, and all the words before the "how" in the decree of the sentence block and all the words after the "how" increment. Each subject has several sentences, and sentences depend on the condition, so the cycle should take into account the instances of the target word for each subject in each sentence. The end result should look something like this.

 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 , , . ? !

+5
3

, , . 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
+4

, .
data.table 21 > ​​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

Udate:

, grepl ==

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

The usual solution with plyr

 ddply(d, .(subj, condition, sentence), transform, 
   position = seq_along(word) - which(word == 'like'))
+3
source

All Articles