Skip to content

separate() right edge fixed position splitting is off by 1 #315

Description

@JoFrhwld

According to the documentation for the sep argument:

If numeric, interpreted as positions to split at. Positive values start at 1 at the far-left of the string; negative value start at -1 at the far-right of the string.

However, to split the right-most character into a new column, sepmust be set to -2. Reproducible example:

library(tidyverse)

df <- expand.grid(group = c("alpha", "beta"),
                  dimension = c(1, 2),
                  stringsAsFactors = F) %>%
        tbl_df() %>%
        mutate(gr_dim = paste0(group, dimension))


# splits the one left most character
df %>%
  separate(gr_dim, sep = 1, into = c("gr", "dim"))

# creates two columns, one of which is the original column, the other blank
df %>%
  separate(gr_dim, sep = -1, into = c("gr", "dim"))

# splits the two left-most characters
df %>%
  separate(gr_dim, sep = 2, into = c("gr", "dim"))

# splits the one right-most character
df %>%
  separate(gr_dim, sep = -2, into = c("gr", "dim"))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorstrings 🎻wipwork in progress

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions