Mixalis Mixalis - 1 year ago 58
R Question

Split String to data frame

I'm trying to split a one-line text in R and store them in a dataframe.

For instance. A text like the following:

hello-world;1|(good)night world;2|...

Is expected to become:

V1 V2
hello-world 1
(good)night world 2

In order to achieve this: I start by splitting the initial text on '\'. For that reason, I use separate from

as.data.frame(str) %>% separate(str, into=c("V1"), sep='\\|')
1 hello-world;1
#Warning message:
#Too many values at 1 locations: 1

I suspect in the first split the issue rises with the
. How can I solve this issue?

Answer Source

If you want to do this via tidyverse then you need to use unnest to make it long and then separate the values i.e.


data.frame(v1 = 'hello-world;1|(good)night world;2|') %>% 
       mutate(v1 = strsplit(as.character(v1), '\\|')) %>% 
       unnest(v1) %>% 
       separate(v1, into = c('v1', 'v2'), sep = ';')

# A tibble: 2 x 2
#                 v1    v2
#*             <chr> <chr>
#1       hello-world     1
#2 (good)night world     2
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download