Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Split a single row into multiple rows keeping the delimiter intact

$
0
0

I am trying to split a single row in my data set into multiple rows by keeping the delimiter intact.

This is a sample of my input data set
|---------------------|----------------------------------------------- |
|      Group          |     Rules                                      |
|---------------------|----------------------------------------------- |
|          1          | 1. Teams must be split into two                |
|                     | 2. Teams must have ten players in each team    | 
|                     | 3. Each player must bring their own gear       |
|---------------------|----------------------------------------------- |

When I use Strsplit function, I get the following output:

df = data.frame(rules =unlist(strsplit(as.character(df$Rules),"?=[[digits]]", perl = T)))

|---------------------|----------------------------------------------- |
|      Group          |     Rules                                      |
|---------------------|----------------------------------------------- |
|          1          | 1                                              |
|--------------------------------------------------------------------- |                           
           1          | .Teams must be split into two                  |
|--------------------------------------------------------------------- |
|          1          | 2                                              |
|--------------------------------------------------------------------- |                           
           1          | .Teams must have ten players in each team      |
|--------------------------------------------------------------------- |

My desired Output

|---------------------|----------------------------------------------- |
|      Group          |     Rules                                      |
|---------------------|----------------------------------------------- |
|           1         | 1.Teams must be split into two                 |
|--------------------------------------------------------------------- |                           
|           1         | 2.Teams must have ten players in each team     |
|--------------------------------------------------------------------- |


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>