【发布时间】:2021-01-14 16:17:56
【问题描述】:
我想根据存储在数据框另一列中的这些子字符串的索引来操作一列中的子字符串:
数据:
df_test
Turn c5 Turns_split
1 we 're not gon na know the person PNP VBB XX0 VVG TO0 VVI AT0 NN1 we, 're, not, gon, na, know, the, person
2 great answer AJ0 NN1 great, answer
3 it 's gon na rain PNP VBZ VVG TO0 VVI it, 's, gon, na, rain
c5_split Index
1 PNP, VBB, XX0, VVG, TO0, VVI, AT0, NN1 4
2 AJ0, NN1
3 PNP, VBZ, VVG, TO0, VVI 3
索引(值4 和3)存储在Index 列中;我要操作的子字符串存储在c5 中,其中包含词性标签。我想做的操作集中在c5 中的两个子字符串上:(i)索引与Index 中的索引值相同的子字符串和(ii)紧随其后的子字符串,即带有Index 值 + 1。我要执行的操作是将两个子字符串之间的空格替换为 = 符号。所以c5 列中的期望输出是这样的:
df_text$c5
"PNP VBB XX0 VVG=TO0 VVI AT0 NN1" "AJ0 NN1" "PNP VBZ VVG=TO0 VVI"
我真的不知道如何做到这一点,因此非常感谢您的指导。
可重复的数据:
df_test <- structure(list(Turn = c("we 're not gon na know the person",
"great answer", "it 's gon na rain"), c5 = c("PNP VBB XX0 VVG TO0 VVI AT0 NN1",
"AJ0 NN1", "PNP VBZ VVG TO0 VVI"), Turns_split = list(c("we",
"'re", "not", "gon", "na", "know", "the", "person"), c("great",
"answer"), c("it", "'s", "gon", "na", "rain")), c5_split = list(
c("PNP", "VBB", "XX0", "VVG", "TO0", "VVI", "AT0", "NN1"),
c("AJ0", "NN1"), c("PNP", "VBZ", "VVG", "TO0", "VVI")), Index = list(
4L, integer(0), 3L)), row.names = c(NA, -3L), class = "data.frame")
【问题讨论】:
-
c5中的字母数字分组是否总是 3 个一组?为什么第一个值改变了?它的索引号是1,但Index的值是4(i) 并且它没有跟随匹配的行 (ii)? -
大多数情况下
c5中的子字符串只有 3 个字符,但有时它们可以具有这样的结构:XXX-XXX,即三个大写字符后跟一个连字符,又是 3 个大写字符。