Split a dataframe column in multiple columns based on multiple occurrences of a separator in R

Pushkar

My data looks like

a  
a >> b  
d >> c >> e 

(unknown times ">>") can be present in a row
And I need them in several columns depending on occurrences of ">>"
I tried strsplit, which converts the column in a list and when I tried to unlist it, it repeats the values like

X1 X2 X3   
a  a  a  
a  b  a  
d  c  e

Is there any Base R solution to get

X1 X2 X3  
a  
a  b  
d  c  e 

And so on? I can only use Base R!!

Gregor Thomas

Assuming your input is a character vector as below, try this:

x = c("a"  ,
"a >> b"  ,
"d >> c >> e")


s = lapply(strsplit(x, split = ">>"), trimws)
s = t(sapply(s, function(x) {length(x) <- max(lengths(s)); x}))
s
#      [,1] [,2] [,3]
# [1,] "a"  NA   NA  
# [2,] "a"  "b"  NA  
# [3,] "d"  "c"  "e"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Split a dataframe in multiple columns in R

From Dev

Split column into multiple columns R

From Dev

Split list in Pandas dataframe column into multiple columns

From Dev

Split column into separate columns based on separator strings

From Dev

pandas dataframe column based on row and multiple columns

From Dev

Split column to multiple columns adaptively in R

From Dev

r: split column into multiple columns by value

From Dev

Split irregular text column into multiple columns in r

From Dev

How to split a character column into multiple columns in R

From Dev

Split column into multiple columns

From Dev

Split a column to multiple columns

From Dev

Split multiple column into two columns based on first column

From Dev

R aggregate based on multiple columns and then merge into dataframe?

From Dev

Merging multiple columns in a dataframe based on condition in R

From Dev

Create a column based on multiple columns- in R

From Dev

Create multiple columns in R based on other column

From Dev

Create multiple columns in R based on other column

From Java

Split pandas dataframe into multiple dataframes based on null columns

From Dev

Split pandas dataframe into multiple dataframes based on null columns

From Dev

Split a single column into multiple columns based on a set of values

From Dev

How to split a dataframe column into multiple columns with a Pandas converter

From Dev

Split a text(with names and values) column into multiple columns in Pandas DataFrame

From Dev

Need to split variable length data in a pandas dataframe column into multiple columns

From Dev

Split one column into multiple columns

From Dev

split xml column into multiple columns

From Dev

Split Column value into multiple columns

From Dev

Mysql split column into multiple columns

From Dev

Split column values into multiple columns with

From Dev

Split one column into multiple columns

Related Related

  1. 1

    Split a dataframe in multiple columns in R

  2. 2

    Split column into multiple columns R

  3. 3

    Split list in Pandas dataframe column into multiple columns

  4. 4

    Split column into separate columns based on separator strings

  5. 5

    pandas dataframe column based on row and multiple columns

  6. 6

    Split column to multiple columns adaptively in R

  7. 7

    r: split column into multiple columns by value

  8. 8

    Split irregular text column into multiple columns in r

  9. 9

    How to split a character column into multiple columns in R

  10. 10

    Split column into multiple columns

  11. 11

    Split a column to multiple columns

  12. 12

    Split multiple column into two columns based on first column

  13. 13

    R aggregate based on multiple columns and then merge into dataframe?

  14. 14

    Merging multiple columns in a dataframe based on condition in R

  15. 15

    Create a column based on multiple columns- in R

  16. 16

    Create multiple columns in R based on other column

  17. 17

    Create multiple columns in R based on other column

  18. 18

    Split pandas dataframe into multiple dataframes based on null columns

  19. 19

    Split pandas dataframe into multiple dataframes based on null columns

  20. 20

    Split a single column into multiple columns based on a set of values

  21. 21

    How to split a dataframe column into multiple columns with a Pandas converter

  22. 22

    Split a text(with names and values) column into multiple columns in Pandas DataFrame

  23. 23

    Need to split variable length data in a pandas dataframe column into multiple columns

  24. 24

    Split one column into multiple columns

  25. 25

    split xml column into multiple columns

  26. 26

    Split Column value into multiple columns

  27. 27

    Mysql split column into multiple columns

  28. 28

    Split column values into multiple columns with

  29. 29

    Split one column into multiple columns

HotTag

Archive