I have a dataframe that looks like the following:
Name Value
abc Asia
def Asia/Africa
gbc Africa
jhg America/Africa/Asia
I want the column Value to be separated so that the dataframe looks as follows:
Name Value.1 Value.2 Value.3
abc Asia
def Asia Africa
gbc Africa
jhg America Africa Asia
I know there are similar questions on stackoverflow (e.g. here, here ) but they assume that Value can be split into the same number of sections for each row. So, when I try to run the following commands:
out <- strsplit(as.character(df$Value),'/')
do.call(rbind, out)
data.frame(df$Value, do.call(rbind, out))
It works till the second line and then gives me the following error:
Error in data.frame(df$Value, do.call(rbind, :
arguments imply differing number of rows: 24819, 24707
In addition: Warning message:
In (function (..., deparse.level = 1) :
number of columns of result is not a multiple of vector length (arg 10)
I also looked into reshape2 function colSplit and tidyr function separate. They also expect that for every row, Value can be seperated into exactly the same number of components, hence they expect me to name the columns in advance.
I am wondering of there is a way to adaptively name split the Value into separate columns called Value.1, Value.2,...