Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从R中的数字中选择第N位?_R - Fatal编程技术网

如何从R中的数字中选择第N位?

如何从R中的数字中选择第N位?,r,R,给定一个至少有2个数字的数字,我如何选择一个特定的数字 x <- 4856 nth_digit(x, 4) 6 x我们可以使用substr,指定start/stop作为要提取的数字的位置。它以character类的形式返回输出,我们将其转换为integer,并使用as.integer as.integer(substr(x, 4, 4)) #[1] 6 或使用sub sub('^.{3}(.)', '\\1', x) 库(stringr) x

给定一个至少有2个数字的数字,我如何选择一个特定的数字

x <- 4856

nth_digit(x, 4)

6

x我们可以使用
substr
,指定
start/stop
作为要提取的数字的位置。它以
character
类的形式返回输出,我们将其转换为
integer
,并使用
as.integer

as.integer(substr(x, 4, 4))
#[1] 6

或使用
sub

sub('^.{3}(.)', '\\1', x)
库(stringr)
x