{"action":"opened","issue":{"url":"https://api.github.com/repos/jjallaire/rcpp-gallery/issues/66","labels_url":"https://api.github.com/repos/jjallaire/rcpp-gallery/issues/66/labels{/name}","comments_url":"https://api.github.com/repos/jjallaire/rcpp-gallery/issues/66/comments","events_url":"https://api.github.com/repos/jjallaire/rcpp-gallery/issues/66/events","html_url":"https://github.com/jjallaire/rcpp-gallery/issues/66","id":63076220,"number":66,"title":"Possible bug in example split_indices function","user":{"login":"jtoll","id":1028108,"avatar_url":"https://avatars.githubusercontent.com/u/1028108?v=3","gravatar_id":"","url":"https://api.github.com/users/jtoll","html_url":"https://github.com/jtoll","followers_url":"https://api.github.com/users/jtoll/followers","following_url":"https://api.github.com/users/jtoll/following{/other_user}","gists_url":"https://api.github.com/users/jtoll/gists{/gist_id}","starred_url":"https://api.github.com/users/jtoll/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jtoll/subscriptions","organizations_url":"https://api.github.com/users/jtoll/orgs","repos_url":"https://api.github.com/users/jtoll/repos","events_url":"https://api.github.com/users/jtoll/events{/privacy}","received_events_url":"https://api.github.com/users/jtoll/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-03-19T19:45:37Z","updated_at":"2015-03-19T19:45:37Z","closed_at":null,"body":"While reading through [_posts/2013-12-02-plyr-c-to-rcpp.md](http://gallery.rcpp.org/articles/plyr-c-to-rcpp/) trying to make sense of the C++ code and the intended output of the `split_indices` function, I may have found a small bug.  I ended up running the function to get an idea of the output it generated and was surprised by the result.\r\n\r\nCurrently, it appears that unless the `IntegerVector x` passed into the function is presorted from min to max, or `n = max(x)`, then the list returned by the function is truncated by `ids.resize(x[i])` to a length that is the value of the final element of `x`. For example, `split_indices(sample(100))` typically results in lists that are less than 100 elements long despite no replacement, whereas `split_indices(sample(100), n = 100)` returns the value I would expect.\r\n\r\nI'm not really sure what the intended output is supposed to be, but rather than:\r\n\r\n```\r\nif (x[i] > n) {\r\n    ids.resize(x[i]);\r\n }\r\n```\r\n\r\nperhaps the code should be:\r\n\r\n```\r\nif (x[i] > ids.size()) {\r\n    ids.resize(x[i]);\r\n}\r\n```\r\n\r\nThen the length of `ids` will only be extended via `resize` rather than possibly truncated in the situation where `x[i] > n` but `x[i] < ids.size()`.\r\n\r\nAdditionally, there are two references to `out` in the second bullet point under the example C++ code which should probably be changed to `ids`.\r\n\r\nThanks"}}