在R上使用extract()提取区号 调用%extract(col=caller\u id\u number,into=“area\u code”,regex=“([\\012-]+)”,remove=F) 查看(通话)
使用R上的提取功能,我想提取电话号码的区号。例如:394-000-0000,我想提取出394。问题是,我不知道如何在extract()函数中设置regex来识别区号的模式。有什么建议吗?谢谢。在regex在R上使用extract()提取区号 调用%extract(col=caller\u id\u number,into=“area\u code”,regex=“([\\012-]+)”,remove=F) 查看(通话),r,R,使用R上的提取功能,我想提取电话号码的区号。例如:394-000-0000,我想提取出394。问题是,我不知道如何在extract()函数中设置regex来识别区号的模式。有什么建议吗?谢谢。在regex[0-9]中匹配0到9之间的所有数字,我们可以用大括号给出准确的数量。因此模式[0-9]{3}将正好匹配三位数字stringr::str_extract()将为您提供第一个匹配项。因此,这应该是可行的: calls <- calls %>% extract(col = caller_
[0-9]
中匹配0到9之间的所有数字,我们可以用大括号给出准确的数量。因此模式[0-9]{3}
将正好匹配三位数字<默认情况下,code>stringr::str_extract()将为您提供第一个匹配项。因此,这应该是可行的:
calls <- calls %>% extract(col = caller_id_number, into = "area_code", regex = "([\\012-]+)", remove = F)
view(calls)
在正则表达式中,
[0-9]
匹配从0到9的所有数字,我们可以用大括号给出精确的数量。因此模式[0-9]{3}
将正好匹配三位数字<默认情况下,code>stringr::str_extract()将为您提供第一个匹配项。因此,这应该是可行的:
calls <- calls %>% extract(col = caller_id_number, into = "area_code", regex = "([\\012-]+)", remove = F)
view(calls)
我们可以使用
base R
来执行此操作
library(stringr)
calls %>%
mutate(area_code = str_extract(caller_id_number, "[0-9]{3}"))
调用$area\u code我们可以使用base R
来执行此操作
library(stringr)
calls %>%
mutate(area_code = str_extract(caller_id_number, "[0-9]{3}"))
调用$area\u code如果它始终是前3位,为什么不使用stringr
库中更简单的stru sub
?@D.J前三位可能不是第一个字符。。。codermcgee-您的一些电话号码是否有括号?如果总是前3位,为什么不使用stringr
库中更简单的stru sub
?@D.J前三位可能不是第一个字符。。。codermcgee-您的一些电话号码是否有括号?