gpt4 book ai didi

r - 在 R 中按顺序标记句子?

转载 作者:行者123 更新时间:2023-12-02 09:13:28 25 4
gpt4 key购买 nike

我有一个单词列表,我想将它们组合成句子。数据目前采用以下格式:

df <- data_frame(word = c("I'm", "going", "to", "be", "sick", "I", "want", "to", "go", "home"),
stop = c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE))

我想按顺序将每个停用词后面的每个句子标记到一个新列中,以便数据如下所示:

df2 <- data_frame(word = c("I'm", "going", "to", "be", "sick", "I", "want", "to", "go", "home"),
stop = c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE),
num = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2))

有什么快速的方法可以做到这一点吗?谢谢!

最佳答案

library(tidyverse)
df %>% mutate(num = cumsum(lag(stop, default = FALSE))+1)
# A tibble: 10 x 3
word stop num
<chr> <lgl> <dbl>
1 I'm FALSE 1.
2 going FALSE 1.
3 to FALSE 1.
4 be FALSE 1.
5 sick TRUE 1.
6 I FALSE 2.
7 want FALSE 2.
8 to FALSE 2.
9 go FALSE 2.
10 home TRUE 2.

关于r - 在 R 中按顺序标记句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49493878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com