如何从R中的数字中选择第N位?
给定一个至少有2个数字的数字,我如何选择一个特定的数字如何从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
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