【发布时间】:2020-06-07 07:48:00
【问题描述】:
我对数据框的 1 个特定列感兴趣,其中每一行包含一个社区的名称和分配给该社区的特定编号。
TOR - HOOD - Banbury-Don Mills (42) (23.6%)
请查看此图片以更好地理解 neighborhoodnum
我只想提取第一个括号内的数字。 TOR - HOOD - Alderwood (20) (25.4%)
我尝试过使用 stringr 包,但所有函数一次只需要 1 个字符串。此列中有 140 行,我想要所有行中的值。我不确定如何遍历列中的每个字符串
Here is what I have tried and the results
以及我使用的一些代码,但出现此错误(UseMethod("type") 中的错误:没有适用于 'type' 的适用方法应用于类 "c('tbl_df', 'tbl', ' data.frame')")
hood_data<-tibble(hood=demo_edu_dataset$Geography)
head(hood_data)
hoodnum<-hood_data %>%
#separate(hood, into= c("name", "number"), sep = "")
stringr::str_extract_all(hood_data, "\\d")
谢谢
【问题讨论】:
-
可能是
stringr::str_extract(hood_data, "(?<=\\()\\d+(?=\\))")? -
您好,法利亚·坎达克。欢迎来到 StackOverflow!请不要在此处发布代码或数据的图像。请阅读有关how to ask a good question 的信息以及如何提供minimale reproducible example。这样你就可以帮助别人帮助你!
标签: r regex stringr data-extraction