[R] cut - strange NA as output
Marc Schwartz
marc_schwartz at me.com
Tue Oct 6 21:57:59 CEST 2015
> On Oct 6, 2015, at 2:20 PM, Hermann Norpois <hnorpois at gmail.com> wrote:
>
> Hello,
>
> why do I get NA for the following:
>
> cut (x, seq (0, max(x), by=1), label=FALSE)
> [1] 1322 1175 1155 1149 1295 1173 1289 1197 NA 1129
>
> dput (x)
> c(1321.55376901374, 1174.35657200935, 1154.02042504008, 1148.60981925942,
> 1294.6166388941, 1172.45806806869, 1288.31933914639, 1196.26080041462,
> 1355.88836502166, 1128.09901883228)
>
> Thanks
> Hermann
> max(x)
[1] 1355.888
> range(seq(0, max(x), by = 1))
[1] 0 1355
max(x) is outside (above) the range of the integer sequence of break points for cut() that you specified above. Thus, when cut() gets to the 9th element in x, the value is undefined.
> cut (x, seq(0, max(x) + 1, by = 1), label=FALSE)
[1] 1322 1175 1155 1149 1295 1173 1289 1197 1356 1129
or
> cut (x, seq(0, ceiling(max(x)), by = 1), label=FALSE)
[1] 1322 1175 1155 1149 1295 1173 1289 1197 1356 1129
Both of the above approaches will increment the sequence 0:max(x) to 1356:
> range(seq(0, max(x) + 1, by = 1))
[1] 0 1356
> range(seq(0, ceiling(max(x)), by = 1))
[1] 0 1356
Regards,
Marc Schwartz
More information about the R-help
mailing list