Search code examples
rdataframemultiple-columns

split columns in "the middle" in R


I have an example data frame as such:

df_1 <- as.data.frame(cbind(c(14, 27, 38), c(25, 33, 52), c(85, 12, 23)))

Now, I want to split all these columns down the middle so that i get something that would look like this:

df_2 <- as.data.frame(cbind(c(1, 2, 3), c(4, 7, 8), c(2,3,5), c(5, 3, 2), c(8, 1, 2), c(5, 2, 3)))

So my question then is: Is there a command/package that can do this automatically?

In my real data frame I am looking to split columns by name, from an earlier regression where i got the names by inserting: paste0(names(df)[i], "~", names(df)[j]) into my loop. My thought, however, is that this will be quite easy once i find the right command for the data frames given above.

Thanks in advance!


Solution

  • Thanks for the answers, they were a lot of help! I ended up using the tidyr package with command:

    test <- as.data.frame(separate(data = test, col = "V1", into = c("col_1", "col_2"), sep = "\\~"))
    

    This worked great for me since I ran a regression earlier and had a good operator for separation: "~"