I've tried to create a function that replaces semicolon-containing elements in a dataframe column with splitted entries that are places on the bottom of the column, using basic R. The main purpose is to use this function with apply and make the addition whenever detecting an entry with semicolon.
The main problem with my code is that it returns the exact same data frame without any additional values.
> df
rs2480711
rs74832092
rs4648658
rs4648659
rs61763535
rs28733941;rs67677371
>x
"rs28733941;rs67677371"
function(x){
semiCols = length(unlist(strsplit(x, ";")))
elementsRs = unlist(strsplit(x, ";"))
if(semiCols>1){
for(i in 1:semiCols){
df = rbind(df, elementsRs[i])
}}}
I would also like to know how can I expand the code in order to split rows based on one value leaving all the others unchanged. For example, this
>df
0 rs61763535 T1
1 rs28733941;rs67677371 T2
will look like this
>df2
0 rs61763535 T1
1 rs28733941 T2
1 rs67677371 T2