I have a data.frame in R, which, for simplicity, has one column that I want to separate. It looks like this:
V1
Value_is_the_best_one
This_is_the_prettiest_thing_I've_ever_seen
Here_is_the_next_example_of_what_I_want
My real data is very large (millions of rows), so I'd like to use tidyr's separate function (because it's amazingly fast) to separate out JUST the first few instances. I'd like the result to be the following:
V1 V2 V3 V4
Value is the best_one
This is the prettiest_thing_I've_ever_seen
Here is the next_example_of_what_I_want
As you can see, the separator is _
the V4 column can have different numbers of the separators. I want to keep V4 (not discard it), but not have to worry about how much stuff is in there. There will always be four columns (i.e. none of my rows have only V1-V3).
Here is my starting tidyr command I've been working with:
separate(df, V1, c("V1", "V2", "V3", "V4"), sep="_")
This gets rid of V4 (and spits out warnings, which isn't the biggest deal).
You need the extra
argument with the "merge"
option. This allows only as many splits as you have new columns defined.
separate(df, V1, c("V1", "V2", "V3", "V4"), extra = "merge")
V1 V2 V3 V4
1 Value is the best_one
2 This is the prettiest_thing_I've_ever_seen
3 Here is the next_example_of_what_I_want
Here is another option with extract
library(tidyr)
extract(df1, V1, into = paste0("V", 1:4), "([^_]+)_([^_]+)_([^_]+)_(.*)")
# V1 V2 V3 V4
# 1 Value is the best_one
# 2 This is the prettiest_thing_I've_ever_seen
# 3 Here is the next_example_of_what_I_want
Another option is stri_split
from library(stringi)
where we can specify the number of splits
library(stringi)
do.call(rbind, stri_split(df1$V1, fixed="_", n=4))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With