R 如何使用“提取数字部分”;";一串
我有一个类似“反射率刻度=5.01129178E-05”的字符串。 我想以数字形式提取零件“5.011129178e-05” 我试过:R 如何使用“提取数字部分”;";一串,r,regex,string,R,Regex,String,我有一个类似“反射率刻度=5.01129178E-05”的字符串。 我想以数字形式提取零件“5.011129178e-05” 我试过: gsub("[^0-9.]", "", "reflectance_scales=5.011129178e-05") 但是它只提取5.011129178,而不是e-05。我们可以使用sub提取数字,并使用eval(parse将其转换为数字 eval(parse(text = sub(".*?(\\d.*)", "\\1", "reflectance_scal
gsub("[^0-9.]", "", "reflectance_scales=5.011129178e-05")
但是它只提取5.011129178,而不是e-05。我们可以使用
sub
提取数字,并使用eval(parse
将其转换为数字
eval(parse(text = sub(".*?(\\d.*)", "\\1", "reflectance_scales=5.011129178e-05")))
#[1] 0.000050111
如果您没有表示数字的科学格式,这也应该起作用
eval(parse(text = sub(".*?(\\d.*)", "\\1", "reflectance_scales=5.0123")))
#[1] 5.0123
您可以使用相同的代码而不使用
eval
parse
,也可以将其包装为.numeric
as.numeric(sub(".*?(\\d.*)", "\\1", "reflectance_scales=5.011129178e-05"))
#[1] 0.000050111
我们可以使用
sub
提取数字,并使用eval(parse
将其转换为数字
eval(parse(text = sub(".*?(\\d.*)", "\\1", "reflectance_scales=5.011129178e-05")))
#[1] 0.000050111
如果您没有表示数字的科学格式,这也应该起作用
eval(parse(text = sub(".*?(\\d.*)", "\\1", "reflectance_scales=5.0123")))
#[1] 5.0123
您可以使用相同的代码而不使用
eval
parse
,也可以将其包装为.numeric
as.numeric(sub(".*?(\\d.*)", "\\1", "reflectance_scales=5.011129178e-05"))
#[1] 0.000050111
与@RonakShah的答案相比,它不那么直截了当,使用@llllliill的思想,您可以分两步完成(不使用正则表达式): 反射比例尺=5.01129178E-05 numb=as.numeric(未列出(strsplit(单词“=”)) 麻木的,麻木的 并获得:
>str(numb)
编号5.01e-05
没有@RonakShah的答案那么直截了当,使用@lllll iill的思想,您可以分两步完成(无需正则表达式):
反射比例尺=5.01129178E-05
numb=as.numeric(未列出(strsplit(单词“=”))
麻木的,麻木的
并获得:
>str(numb)
编号5.01e-05
您可以试试
r <- gsub("\\w.*?=", "", "reflectance_scales=5.011129178e-05")
如果您希望将其作为数字类型,则使用作为.numeric(r)
您可以尝试
r <- gsub("\\w.*?=", "", "reflectance_scales=5.011129178e-05")
如果要将其作为数值类型,请使用作为.numeric(r)
基本r解决方案:
# Solution 1, string split, unlist, coerce to numeric, subset out NAs, coerce to numeric,
# format with scientific notation (type coercion to string):
format(as.numeric(na.omit(as.numeric(unlist(strsplit(X, "="))))), scientific = TRUE)
# Solution 2, substitution of alphabetic characters coercion to numeric
# format with scientific notation (type coercion to string)
format(as.numeric(gsub("[^0-9]+", "", X)), scientific = TRUE)
# Solution 3, string split, digit extraction:
grep("\\d+", unlist(strsplit(X, "=")), value = TRUE)
数据:
X <- as.character("reflectance_scales=5.011129178e-05")
XBase R解决方案:
# Solution 1, string split, unlist, coerce to numeric, subset out NAs, coerce to numeric,
# format with scientific notation (type coercion to string):
format(as.numeric(na.omit(as.numeric(unlist(strsplit(X, "="))))), scientific = TRUE)
# Solution 2, substitution of alphabetic characters coercion to numeric
# format with scientific notation (type coercion to string)
format(as.numeric(gsub("[^0-9]+", "", X)), scientific = TRUE)
# Solution 3, string split, digit extraction:
grep("\\d+", unlist(strsplit(X, "=")), value = TRUE)
数据:
X <- as.character("reflectance_scales=5.011129178e-05")
X或者像解析列表定义一样解析它:
x[1]5.011129e-05
由(v0.3.0)于2019-12-04创建
与Ronak的答案相比,这是非常有效的:
x#A tibble:2x6
#>表达式最小中位数`itr/sec`mem_alloc`gc/sec`
#>
#>1毫米5.9us 7.8us 72889.2.08KB 7.29
#>2罗纳克29.6us 31.4us 24103.280B 9.65
由(v0.3.0)于2019-12-04创建,或将其作为列表定义进行解析:
x[1]5.011129e-05
由(v0.3.0)于2019-12-04创建
与Ronak的答案相比,这是非常有效的:
x#A tibble:2x6
#>表达式最小中位数`itr/sec`mem_alloc`gc/sec`
#>
#>1毫米5.9us 7.8us 72889.2.08KB 7.29
#>2罗纳克29.6us 31.4us 24103.280B 9.65
由(v0.3.0)于2019-12-04创建的(v0.3.0)数字是否始终采用科学格式?您也可以使用split()
和=
作为delimeter。@stud3我要求的是一般情况,而不是具体情况。split()
不适用于每种情况。数字是否始终采用科学格式?您也可以使用split()
使用=
作为delimeter。@stud3我要求的是一般情况,而不是具体情况。split()
不适用于每种情况。