为什么 substr() return 是一个空字符串?

Why does substr() return an empty string?

我不知道我是否有逻辑错误,但我的 substr() 函数一直返回空字符串?我最初的想法是我把字符串剪错了,但是即使起始值较低,我的数据框列中也会出现空字符串。

我查看了这个 PHP 问题以获取一些信息,但没有成功:PHP: substr returns empty string

可重现的例子-

#Original Data set str() output
#'data.frame':  9245 obs. of  3 variables:
#$ Latitude   : num  29.7 29.7 29.7 29.6 29.7 ...
#$ Longitude  : num  -82.3 -82.4 -82.3 -82.4 -82.3 ...
#$ Census Code: chr  "120010011003032" "120010010004035" "120010002003009" "120010015213000" ...

#For example, even if I do this:
base::substr("120010011003032", 6, 1)

#Output : ""
#Desired output: 001100

我需要切割人口普查代码来生成区域信息,区域信息通常是前两个是州,接下来三个是县,接下来六个是区域。

你需要base::substr("120010011003032", 1, 6)! (编辑:或 11, 6 根据您的评论)

参数是start, stop而不是length, start;请参阅 the doc 或键入 ?base::substr。提示:始终首先对 R 文档进行三次检查。并尝试复制粘贴它提供给您的工作示例,然后查看 if/how 它们与您的不同。或者只是尝试各种参数值。

base::substr("120010011003032", 6, 11)

"001100"