从13位数字中提取第7个数字

时间:2016-01-27 08:29:38

标签: r

我想从13位数字中提取第7个数字。例如9111110123341,0将是第7个数字,然后我会有一个if语句,说明数字是否在0到5之间,将m分配给新列。在r中有可能吗?

2 个答案:

答案 0 :(得分:8)

不要将文本处理功能用于数字操作:

extract_digit <- function(x, n) floor(x / 10^(ceiling(log10(x)) - n )) -  
  floor(x / 10^(ceiling(log10(x)) - n + 1)) * 10
extract_digit(9111110123341, 7:10) #extract the 7th to 10th digits
#[1] 0 1 2 3

答案 1 :(得分:3)

你可以尝试

number7 = substr("9111110123341",7,7)
相关问题