I have a datatable and for each datatable$Ppt and each datatable$nitem, when there is "fffword" in datatable$Region, I need to extract the value of "fffword" and compare it with the value of the following "word". If the two values are the same, then I need a value of 0 in datatable$Output, if the two values are different, I need a value of 1 in datatable$Output.
I have tried with:
datatable %>% group_by(Ppt, nitem) %>%
mutate(Output = ifelse(as.numeric(gsub("fffword([0-9]+).*","\\1",Region) == lag(as.numeric(gsub("word([0-9]+).*","\\1",Region)), 0L,ifelse(as.numeric(gsub("fffword([0-9]+).*","\\1",Region) != lag(as.numeric(gsub("word([0-9]+).*","\\1",Region)), 1L)
but it doesn't work.
#Ppt Region nitem Output
#1 "fffword8" 93 0
#1 "word8" 93 0
#1 "fffword9" 93 1
#1 "word2" 122 1
#1 "fffword3" 122 0
#1 "word3" 122 0
#1 "word6" 122 1
#1 "fffword7" 122 0
#1 "word7" 122 0
#1 "fffword8" 122 0
#1 "word8" 122 0
#54 "fffword8" 4 0
#54 "word8" 4 0
#54 "fffword9" 4 1
#54 "word2" 4 1
#54 "fffword2" 10 0
#54 "word4" 10 0
#54 "word6" 10 1
#54 "fffword23" 10 0
#54 "word23" 10 0
#54 "fffword24" 5 0
#54 "word24" 5 0
Aucun commentaire:
Enregistrer un commentaire