Skip to main content
deleted 120 characters in body
Source Link
benson23
  • 19.6k
  • 9
  • 25
  • 44

The separate function is superseded by separate_wider_position() and separate_wider_delim().

For your case, you can use separate_wider_position() from the tidyr package. The widths_vec controls the name and width of the newly created columns, and you want a width of 1.

EDIT: my previous separate() doesn't give the correct output, now deleted, please use separate_wider_position().

library(tidyverse)

x <- c(1,2,12,31,123,2341)
df <- data.frame(x)

widths_vec <- setNames(rep(1, max(nchar(x))), letters[1:max(nchar(x))])

df %>% separate_wider_position(cols = x, widths = widths_vec, too_few = "align_start", cols_remove = F)

# A tibble: 6 × 5
  a     b     c     d         x
  <chr> <chr> <chr> <chr> <dbl>
1 1     NA    NA    NA        1
2 2     NA    NA    NA        2
3 1     2     NA    NA       12
4 3     1     NA    NA       31
5 1     2     3     NA      123
6 2     3     4     1      2341

The separate function is superseded by separate_wider_position() and separate_wider_delim().

For your case, you can use separate_wider_position() from the tidyr package. The widths_vec controls the name and width of the newly created columns, and you want a width of 1.

EDIT: my previous separate() doesn't give the correct output, now deleted, please use separate_wider_position().

library(tidyverse)

x <- c(1,2,12,31,123,2341)
df <- data.frame(x)

widths_vec <- setNames(rep(1, max(nchar(x))), letters[1:max(nchar(x))])

df %>% separate_wider_position(cols = x, widths = widths_vec, too_few = "align_start", cols_remove = F)

# A tibble: 6 × 5
  a     b     c     d         x
  <chr> <chr> <chr> <chr> <dbl>
1 1     NA    NA    NA        1
2 2     NA    NA    NA        2
3 1     2     NA    NA       12
4 3     1     NA    NA       31
5 1     2     3     NA      123
6 2     3     4     1      2341

The separate function is superseded by separate_wider_position() and separate_wider_delim().

For your case, you can use separate_wider_position() from the tidyr package. The widths_vec controls the name and width of the newly created columns, and you want a width of 1.

library(tidyverse)

x <- c(1,2,12,31,123,2341)
df <- data.frame(x)

widths_vec <- setNames(rep(1, max(nchar(x))), letters[1:max(nchar(x))])

df %>% separate_wider_position(cols = x, widths = widths_vec, too_few = "align_start", cols_remove = F)

# A tibble: 6 × 5
  a     b     c     d         x
  <chr> <chr> <chr> <chr> <dbl>
1 1     NA    NA    NA        1
2 2     NA    NA    NA        2
3 1     2     NA    NA       12
4 3     1     NA    NA       31
5 1     2     3     NA      123
6 2     3     4     1      2341
Notice added Recommended answer in R Language by M--
added 34 characters in body
Source Link
benson23
  • 19.6k
  • 9
  • 25
  • 44

The separate function is superseded by separate_wider_position() and separate_wider_delim().

For your case, you can use separate_wider_position() from the tidyr package. The widths_vec controls the name and width of the newly created columns, and you want a width of 1.

EDIT: my previous separate() doesn't give the correct output, now deleted, please use separate_wider_position().

library(tidyverse)

x <- c(1,2,12,31,123,2341)
df <- data.frame(x)

widths_vec <- setNames(rep(1, max(nchar(x))), letters[1:max(nchar(x))])

df %>% separate_wider_position(cols = x, widths = widths_vec, too_few = "align_start", cols_remove = F)

# A tibble: 6 × 5
  a     b     c     d         x
  <chr> <chr> <chr> <chr> <dbl>
1 1     NA    NA    NA        1
2 2     NA    NA    NA        2
3 1     2     NA    NA       12
4 3     1     NA    NA       31
5 1     2     3     NA      123
6 2     3     4     1      2341

You can also use the old separate() as follows:

df %>% separate(col = x, into = letters[1:(max(nchar(x)) + 1)], sep = "", remove = F)

     x a b    c    d    e
1    1   1 <NA> <NA> <NA>
2    2   2 <NA> <NA> <NA>
3   12   1    2 <NA> <NA>
4   31   3    1 <NA> <NA>
5  123   1    2    3 <NA>
6 2341   2    3    4    1

The separate function is superseded by separate_wider_position() and separate_wider_delim().

For your case, you can use separate_wider_position() from the tidyr package. The widths_vec controls the name and width of the newly created columns, and you want a width of 1.

library(tidyverse)

x <- c(1,2,12,31,123,2341)
df <- data.frame(x)

widths_vec <- setNames(rep(1, max(nchar(x))), letters[1:max(nchar(x))])

df %>% separate_wider_position(cols = x, widths = widths_vec, too_few = "align_start", cols_remove = F)

# A tibble: 6 × 5
  a     b     c     d         x
  <chr> <chr> <chr> <chr> <dbl>
1 1     NA    NA    NA        1
2 2     NA    NA    NA        2
3 1     2     NA    NA       12
4 3     1     NA    NA       31
5 1     2     3     NA      123
6 2     3     4     1      2341

You can also use the old separate() as follows:

df %>% separate(col = x, into = letters[1:(max(nchar(x)) + 1)], sep = "", remove = F)

     x a b    c    d    e
1    1   1 <NA> <NA> <NA>
2    2   2 <NA> <NA> <NA>
3   12   1    2 <NA> <NA>
4   31   3    1 <NA> <NA>
5  123   1    2    3 <NA>
6 2341   2    3    4    1

The separate function is superseded by separate_wider_position() and separate_wider_delim().

For your case, you can use separate_wider_position() from the tidyr package. The widths_vec controls the name and width of the newly created columns, and you want a width of 1.

EDIT: my previous separate() doesn't give the correct output, now deleted, please use separate_wider_position().

library(tidyverse)

x <- c(1,2,12,31,123,2341)
df <- data.frame(x)

widths_vec <- setNames(rep(1, max(nchar(x))), letters[1:max(nchar(x))])

df %>% separate_wider_position(cols = x, widths = widths_vec, too_few = "align_start", cols_remove = F)

# A tibble: 6 × 5
  a     b     c     d         x
  <chr> <chr> <chr> <chr> <dbl>
1 1     NA    NA    NA        1
2 2     NA    NA    NA        2
3 1     2     NA    NA       12
4 3     1     NA    NA       31
5 1     2     3     NA      123
6 2     3     4     1      2341
Source Link
benson23
  • 19.6k
  • 9
  • 25
  • 44

The separate function is superseded by separate_wider_position() and separate_wider_delim().

For your case, you can use separate_wider_position() from the tidyr package. The widths_vec controls the name and width of the newly created columns, and you want a width of 1.

library(tidyverse)

x <- c(1,2,12,31,123,2341)
df <- data.frame(x)

widths_vec <- setNames(rep(1, max(nchar(x))), letters[1:max(nchar(x))])

df %>% separate_wider_position(cols = x, widths = widths_vec, too_few = "align_start", cols_remove = F)

# A tibble: 6 × 5
  a     b     c     d         x
  <chr> <chr> <chr> <chr> <dbl>
1 1     NA    NA    NA        1
2 2     NA    NA    NA        2
3 1     2     NA    NA       12
4 3     1     NA    NA       31
5 1     2     3     NA      123
6 2     3     4     1      2341

You can also use the old separate() as follows:

df %>% separate(col = x, into = letters[1:(max(nchar(x)) + 1)], sep = "", remove = F)

     x a b    c    d    e
1    1   1 <NA> <NA> <NA>
2    2   2 <NA> <NA> <NA>
3   12   1    2 <NA> <NA>
4   31   3    1 <NA> <NA>
5  123   1    2    3 <NA>
6 2341   2    3    4    1