gpt4 book ai didi

r - R 中用于返回正则表达式中匹配的单词数的语法是什么?

转载 作者:行者123 更新时间:2023-12-05 09:05:14 24 4
gpt4 key购买 nike

R 包:stringr::words

在应用以下正则表达式后,我想知道 stringr::words 文件中恰好三个字母长的单词数:

x <- str_view(words, "^...$", match = TRUE)

虽然代码能够提取恰好三个字母长的单词,但它并没有告诉我有多少单词。所以,我认为长度函数适合查找数字。

length(x)

代码返回 8,这不可能,因为很明显 x 大于 8。

在与正则表达式(在本例中为 x)匹配后计算单词数的正确语法是什么?

此外,谁能向我解释为什么 length(x) 在上面的例子中返回 8?

提前谢谢你。

最佳答案

str_view 返回一个用于查看的 HTML 对象。

x <- str_view(words, "^...$", match = TRUE)
class(x)
#[1] "str_view" "htmlwidget"

您看到的 8 个组件是

names(x)
#[1] "x" "width" "height" "sizingPolicy" "dependencies"
#[6] "elementId" "preRenderHook" "jsHooks"

代替 str_view 使用 str_subset :

library(stringr)

x <- str_subset(words, "^...$")
x

# [1] "act" "add" "age" "ago" "air" "all" "and" "any" "arm" "art" "ask" "bad" "bag"
# [14] "bar" "bed" "bet" "big" "bit" "box" "boy" "bus" "but" "buy" "can" "car" "cat"
# [27] "cup" "cut" "dad" "day" "die" "dog" "dry" "due" "eat" "egg" "end" "eye" "far"
# [40] "few" "fit" "fly" "for" "fun" "gas" "get" "god" "guy" "hit" "hot" "how" "job"
# [53] "key" "kid" "lad" "law" "lay" "leg" "let" "lie" "lot" "low" "man" "may" "mrs"
# [66] "new" "non" "not" "now" "odd" "off" "old" "one" "out" "own" "pay" "per" "put"
# [79] "red" "rid" "run" "say" "see" "set" "sex" "she" "sir" "sit" "six" "son" "sun"
# [92] "tax" "tea" "ten" "the" "tie" "too" "top" "try" "two" "use" "war" "way" "wee"
#[105] "who" "why" "win" "yes" "yet" "you"


length(x)
#[1] 110

关于r - R 中用于返回正则表达式中匹配的单词数的语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67458264/

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