Skip to content

tstrsplit function to strsplit *and* transpose to easily split + update columns directly. #1026

Closed

Description

@arunsrinivasan

This has come up quite frequently on SO. Here is one example.

Conceptually, tstrsplit() is just transpose(strsplit(...), fill=.) where transpose() is implemented in #1025.

require(data.table)
dt <- data.table(category.tag=c("toys/David", "toys/David", "toys/James", "toys", "toys", "toys/James"), transaction=1:6)
#    category.tag transaction
#1:   toys/David           1
#2:   toys/David           2
#3:   toys/James           3
#4:         toys           4
#5:         toys           5
#6:   toys/James           6
dt[, c("category", "tag") := tstrsplit(category.tag, "/", fixed=TRUE)]
#    category.tag transaction category   tag
#1:   toys/David           1     toys David
#2:   toys/David           2     toys David
#3:   toys/James           3     toys James
#4:         toys           4     toys    NA
#5:         toys           5     toys    NA
#6:   toys/James           6     toys James

Additionally we can also pass a fill = argument to tstrsplit.

Activity

  1. added this to the v1.9.6 milestone on Jan 27, 2015
  2. arunsrinivasan commented on Jan 27, 2015

    @arunsrinivasan
    MemberAuthor

    Updated this and this SO posts.

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