Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rmscriven opened this issue Mar 26, 2015 · 2 comments
Closed

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

rmscriven opened this issue Mar 26, 2015 · 2 comments
Assignees
Milestone

Comments

@rmscriven
Copy link

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.

@arunsrinivasan
Copy link
Member

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

@DavidArenburg
Copy link
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)

@arunsrinivasan arunsrinivasan added this to the v1.9.6 milestone Mar 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants