{"action":"created","issue":{"url":"https://api.github.com/repos/Rdatatable/data.table/issues/1543","repository_url":"https://api.github.com/repos/Rdatatable/data.table","labels_url":"https://api.github.com/repos/Rdatatable/data.table/issues/1543/labels{/name}","comments_url":"https://api.github.com/repos/Rdatatable/data.table/issues/1543/comments","events_url":"https://api.github.com/repos/Rdatatable/data.table/issues/1543/events","html_url":"https://github.com/Rdatatable/data.table/issues/1543","id":135850082,"number":1543,"title":"FR: assign argument for tstrsplit","user":{"login":"MichaelChirico","id":7606389,"avatar_url":"https://avatars.githubusercontent.com/u/7606389?v=3","gravatar_id":"","url":"https://api.github.com/users/MichaelChirico","html_url":"https://github.com/MichaelChirico","followers_url":"https://api.github.com/users/MichaelChirico/followers","following_url":"https://api.github.com/users/MichaelChirico/following{/other_user}","gists_url":"https://api.github.com/users/MichaelChirico/gists{/gist_id}","starred_url":"https://api.github.com/users/MichaelChirico/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MichaelChirico/subscriptions","organizations_url":"https://api.github.com/users/MichaelChirico/orgs","repos_url":"https://api.github.com/users/MichaelChirico/repos","events_url":"https://api.github.com/users/MichaelChirico/events{/privacy}","received_events_url":"https://api.github.com/users/MichaelChirico/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-02-23T20:07:04Z","updated_at":"2016-02-24T13:24:22Z","closed_at":null,"body":"It's something of a nuisance to assign ragged columns with `tstrsplit`, if we don't necessarily know how many columns will result _ex ante_. Is it possible to add an `assign` argument which would use `` `:=` `` or `set` to add the result as extra columns to the table?\r\n\r\n```\r\nDT <- data.table(x = c(\"a/b/c/d\", \"a/b/c\", \"a/b\"),\r\n                 y = 1:3, z = 4:6)\r\nDT[ , tstrsplit(x, \"/\")]\r\n#    V1 V2 V3 V4 #`x`, `y`, and `z` are irrecoverable\r\n# 1:  a  b  c NA\r\n# 2:  a  b NA NA\r\n# 3:  a  b  c  d\r\n```\r\n\r\nWorkaround (ugly)\r\n\r\n```\r\nDT[ , paste0(\"V\", 1:max(sapply(spl <- strsplit(x, \"/\"), length))) := transpose(spl)][]\r\n#          x y z V1 V2 V3 V4\r\n# 1:   a/b/c 1 5  a  b  c NA\r\n# 2:     a/b 2 6  a  b NA NA\r\n# 3: a/b/c/d 3 7  a  b  c  d\r\n```\r\n\r\nInspired by [this SO question](http://stackoverflow.com/q/35586492/3576984)"},"comment":{"url":"https://api.github.com/repos/Rdatatable/data.table/issues/comments/188252981","html_url":"https://github.com/Rdatatable/data.table/issues/1543#issuecomment-188252981","issue_url":"https://api.github.com/repos/Rdatatable/data.table/issues/1543","id":188252981,"user":{"login":"MichaelChirico","id":7606389,"avatar_url":"https://avatars.githubusercontent.com/u/7606389?v=3","gravatar_id":"","url":"https://api.github.com/users/MichaelChirico","html_url":"https://github.com/MichaelChirico","followers_url":"https://api.github.com/users/MichaelChirico/followers","following_url":"https://api.github.com/users/MichaelChirico/following{/other_user}","gists_url":"https://api.github.com/users/MichaelChirico/gists{/gist_id}","starred_url":"https://api.github.com/users/MichaelChirico/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MichaelChirico/subscriptions","organizations_url":"https://api.github.com/users/MichaelChirico/orgs","repos_url":"https://api.github.com/users/MichaelChirico/repos","events_url":"https://api.github.com/users/MichaelChirico/events{/privacy}","received_events_url":"https://api.github.com/users/MichaelChirico/received_events","type":"User","site_admin":false},"created_at":"2016-02-24T13:24:22Z","updated_at":"2016-02-24T13:24:22Z","body":"@mrdwab thanks for the feedback! Indeed I had thought about how to implement this a bit but was stymied -- the only other function I know of that accepts can assign things is `:=`, and that _doesn't_ work outside `j`. But I do occasionally use `tstrsplit` on a non-`data.table` object -- it's a nice feature.\r\n\r\nOf course if this is possible this is the way to go:\r\n\r\n```\r\nif (assign){\r\n  # behave like `:=`\r\n}else{\r\n  # behave like now\r\n}\r\n```\r\n\r\nIf not, an alternative would be to add a `keep.n` argument to `transpose` (I see in the `C` code this is just a matter of returning `maxlen`), which works like:\r\n\r\n```\r\nif (keep.n){\r\n  # return length-2 list, first element: as before, named \"list\"; second: paste0(\"V\", 1:maxlen), named \"v.name\"\r\n}else{\r\n  # as before\r\n}\r\n```\r\n\r\nAnd used like\r\n\r\n    DT[ , (x <- tstrsplit(x, \"/\", keep.n = TRUE))$vname := x$list]"}}