Skip to content

Commit

Permalink
Closes #1094. type.convert arg for tstrsplit.
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsrinivasan committed Mar 26, 2015
1 parent d61c343 commit 02b5846
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions R/transpose.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ transpose <- function(l, fill=NA, ignore.empty=FALSE) {
ans[]
}

tstrsplit <- function(..., fill=NA) {
transpose(strsplit(...), fill=fill, ignore.empty = FALSE)
tstrsplit <- function(x, ..., fill=NA, type.convert=FALSE) {
ans = transpose(strsplit(as.character(x), ...), fill=fill, ignore.empty = FALSE)
# Implementing #1094, but default FALSE
if(type.convert) ans = lapply(ans, type.convert, as.is = TRUE)
ans
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* `uniqueN` gains a `by` argument which is equal to `key(x)` when `x` is a `data.table` so that the behaviour is identical to `duplicated()` and and `unique` methods for `data.table`. Thanks to @kevinmistry for the report. Closes [#1080](https://github.com/Rdatatable/data.table/issues/1080).

11. Implemented `transpose()` to transpose a list and `tstrsplit` which is a wrapper for `transpose(strsplit(...))`. This is particularly useful in scenarios where a column has to be split and the resulting list has to be assigned to multiple columns. See `?transpose` and `?tstrsplit`, [#1025](https://github.com/Rdatatable/data.table/issues/1025) and [#1026](https://github.com/Rdatatable/data.table/issues/1026) for usage scenarios. Closes both #1025 and #1026 issues.
* Implemented `type.convert` as suggested by Richard Scriven. Closes [#1094](https://github.com/Rdatatable/data.table/issues/1094).

12. `melt.data.table` can now melt into multiple columns by providing a list of columns to `measure.vars` argument. Closes [#828](https://github.com/Rdatatable/data.table/issues/828). Thanks to Ananda Mahto for the extended email discussions and ideas on generating the `variable` column.

Expand Down
9 changes: 9 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -6256,6 +6256,15 @@ test(1505.1, as.matrix(dt), as.matrix(as.data.frame(dt)))
dt = data.table(x=1, y=2)
test(1506, setcolorder(dt, c("y", "x")), data.table(y=2, x=1))

# tstrsplit, #1094
# factor to character
x = factor(paste(letters[1:5], letters[6:10], sep="-"))
test(1507.1, tstrsplit(x, "-"), list(letters[1:5], letters[6:10]))
# type.convert
x = paste(letters[1:5], 1:5, sep="-")
test(1507.2, tstrsplit(x, "-"), list(letters[1:5], as.character(1:5)))
test(1507.3, tstrsplit(x, "-", type.convert=TRUE), list(letters[1:5], 1:5))

##########################


Expand Down

0 comments on commit 02b5846

Please sign in to comment.