【发布时间】:2018-02-10 06:18:26
【问题描述】:
我有一个数据表,对于每个datatable$Ppt和每个datatable$nitem,当datatable$Region中有“fffword”时,我需要提取“fffword”的值并进行比较具有以下“单词”的值。如果两个值相同,那么我需要datatable$Output中的值0,如果两个值不同,我需要datatable$Output中的值1。
我试过了:
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)
但它不起作用。
#Ppt Region nitem Output
#1 "fffword8" 93 0 (current ffword n=8, following word n=8)
#1 "word8" 93 0 (previous ffword n=8, current word n=8)
#1 "fffword9" 93 1 (current ffword n=9, no following word for this ppt and this nitem)
#1 "word2" 122 1 (no previous fffword for this ppt and this nitem and this n Region)
#1 "fffword3" 122 0 (current ffword n=3, following word n=3)
#1 "word3" 122 0 (previous ffword n=3, current word n=3)
#1 "word6" 122 1 (no previous fffword for this ppt and this nitem and this n Region)
#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 1
#54 "word6" 10 1
#54 "fffword23" 10 0
#54 "word23" 10 0
#54 "fffword24" 5 0
#54 "word24" 5 0
【问题讨论】:
-
结果是否高于您的预期输出
-
@Onyambu 是的,就是这样。
-
在某些情况下,“ffword”后跟 2 个“word”。这是您的流程中断的地方吗?
-
@AntoniosK 我在预期输出旁边添加了解释
-
你的意思是你检查当前和以前和当前?
标签: r if-statement rows