r - Split a column of a data frame to multiple columns -
i'd take data of form
before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2')) attr type 1 1 foo_and_bar 2 30 foo_and_bar_2 3 4 foo_and_bar 4 6 foo_and_bar_2 and use split() on column "type" above this:
attr type_1 type_2 1 1 foo bar 2 30 foo bar_2 3 4 foo bar 4 6 foo bar_2 i came unbelievably complex involving form of apply worked, i've since misplaced that. seemed far complicated best way. can use strsplit below, unclear how 2 columns in data frame.
> strsplit(as.character(before$type),'_and_') [[1]] [1] "foo" "bar" [[2]] [1] "foo" "bar_2" [[3]] [1] "foo" "bar" [[4]] [1] "foo" "bar_2" thanks pointers. i've not quite groked r lists yet.
use stringr::str_split_fixed
library(stringr) str_split_fixed(before$type, "_and_", 2)
Comments
Post a Comment