我有一个像这样的字符串:
years<-c("20 years old", "1 years old")
我想从这个向量中仅提取数字。期望的输出是一个向量:
c(20, 1)
我该怎么做呢?
在来自Gabor Grothendieck的帖子之后,该帖子发布于r-help邮件列表上。
years<-c("20 years old", "1 years old")
library(gsubfn)
pat <- "[-+.e0-9]*\\d"
sapply(years, function(x) strapply(x, pat, as.numeric)[[1]])
我对这个问题很感兴趣,因为它涉及从base::summary()
函数中提取值。另一个你可能想考虑的选项是构建一个函数,它可以将你的summary()
表中的任何条目转换成有用的数字。例如,如果你得到:
(s <- summary(dataset))
sv_final_num_beneficiarios sv_pfam_rec sv_area_transf
Min. : 1.0 Min. :0.0000036 Min. :0.000004
1st Qu.: 67.5 1st Qu.:0.0286363 1st Qu.:0.010107
Median : 200.0 Median :0.0710803 Median :0.021865
Mean : 454.6 Mean :0.1140274 Mean :0.034802
3rd Qu.: 515.8 3rd Qu.:0.1527177 3rd Qu.:0.044234
Max. :17516.0 Max. :0.8217923 Max. :0.360924
你可能想要提取sv_pfam_rec
的1st Qu
,为此可以读取第二行第二列。为了获得格式化的单一值,我创建了一个函数。
s_extract <- function(summary_entry){
separate(as_tibble(summary_entry),
sep = ":",
col = value,
remove = FALSE,
into = c("bad", "good"))[[3]] %>%
as.numeric()
}
你只需要提供一个摘要条目,例如summary_entry = s[3,3]
,就可以获得sv_area_transf
的中位数
。
值得注意的是,由于此函数基于separate()
,因此在变量名称中包含数字的某些情况下,它使得导航更加容易。
years <- c("20 years old", "1 years old")
as.numeric(gsub("[^0-9]", "", years))
#> [1] 20 1
创建于2023-07-24,使用reprex v2.0.2。
这里我们在regex
的开头使用^
来否定模式。