Skip to content

Please add a "type.convert" argument to tstrsplit() #1094

Closed

Description

@rmscriven

In relation to the post

http://stackoverflow.com/questions/29268941/how-to-split-a-string-variable-in-variables-in-r-non-rectangular/29268972#29268972

it would be convenient and useful if there was a type.convert argument to the new tstrsplit() function, for obvious reasons like easy conversion of numerics as characters to actual numerics after splitting.

I was thinking something as simple as

tstrsplit <- function(..., fill = NA, type.convert = TRUE) {
    x <- transpose(strsplit(...), fill = fill, ignore.empty = FALSE)
    if(type.convert) lapply(x, type.convert, as.is = TRUE) else x
}

x <- c("", "1-7-9", "3", "2-4-6-8")
tstrsplit(x, "-", fixed = TRUE)
# [[1]]
# [1] NA  1  3  2
# 
# [[2]]
# [1] NA  7 NA  4
# 
# [[3]]
# [1] NA  9 NA  6
# 
# [[4]]
# [1] NA NA NA  8

But I'm not sure how your internals work and might be better-off done internally. Of course, other things to think about are the other arguments to type.convert(), but they aren't available in read.table() so that might not matter.

Activity

  1. arunsrinivasan commented on Mar 26, 2015

    @arunsrinivasan
    Member

    Also convert from factor to character by default as spotted by @rmscriven here.

  2. DavidArenburg commented on Mar 26, 2015

    @DavidArenburg
    Member

    Also maybe add simplify argument too in case you want a matrix back instead of a list (though it's not a problem when within the data.table parent frame)

  3. added this to the v1.9.6 milestone on Mar 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions