gsub 查找模式
gsub to find a pattern
我的数据是这样的,叫做:df.
"77 173 648 1936"
所有的数字都可以有1-4位的长度。
和
gsub( " .*$", "", df)
#77
我找到了 77。但是如何在这个模式中找到 173?
我们可以使用str_extract_all
来提取所有数字(\d+
)
library(stringr)
lapply(str_extract_all(df1, '\d+'), as.numeric)
或者只是
scan(text=df1, sep=' ', what=numeric(), quiet=TRUE)
#[1] 77 173 648 1936
你也可以使用,
as.numeric(regmatches(df1, gregexpr("[[:digit:]]+", df1))[[1]])
#[1] 77 173 648 1936
我的数据是这样的,叫做:df.
"77 173 648 1936"
所有的数字都可以有1-4位的长度。
和
gsub( " .*$", "", df)
#77
我找到了 77。但是如何在这个模式中找到 173?
我们可以使用str_extract_all
来提取所有数字(\d+
)
library(stringr)
lapply(str_extract_all(df1, '\d+'), as.numeric)
或者只是
scan(text=df1, sep=' ', what=numeric(), quiet=TRUE)
#[1] 77 173 648 1936
你也可以使用,
as.numeric(regmatches(df1, gregexpr("[[:digit:]]+", df1))[[1]])
#[1] 77 173 648 1936