Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there an r function (rstudio) for combining data frames together?

I am trying to combine two data frames together that have identical column and row names, but different values for each point. They are essentially replicate experiments. I’d like to merge by rows, but also group the columns together.

Essentially it looks like this:
Data frame 1:
|__|c1|c2|c3|
|–|–|–|–|
|r1|2 |3 |1 |
|r2|.5|2 |3 |
|r3|3 |4 |2 |

Data frame 2:
|__|c1|c2|c3|
|–|–|–|–|
|r1|3 |5 |3 |
|r2|6 |2 |3 |
|r3|4 |1 |7 |

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and I am trying to get an output which looks like this:
|__|c1|c1|c2|c2|c3|c3|
|–|–|–|–|–|–|–|
|r1|2 |3 |3 |5 |1 |3 |
|r2|.5|6 |2 |2 |3 |3 |
|r3|3 |4 |4 |1 |2 |7 |

I know this is probably a very basic question, but I’m honestly not even sure of the right language to google a solution, and all my attempts to find an answer have been fruitless.

I have already tried using cbind(), rbind(), and merge(), however both gave not quite the correct results.

rbind(data_1, data_2) gave close to my desired output but the columns were not sorted or grouped together.

Essentially it gave an output that looked like this:
|__|c1|c2|c3|c1|c2|c3|
|–|–|–|–|–|–|–|
|r1|2 |3 |1 |3 |5 |3 |
|r2|.5|2 |3 |6 |2 |3 |
|r3|3 |4 |2 |4 |1 |7 |

I thought maybe sort() or grouping() could be a solution, but I was not able to figure out how to use those to get my desired result.

If there is a thread on how to do this, any direction to that thread is appreciated.

Thanks for all your help!

>Solution :

To combine two data frames by grouping columns together, you can use the cbind() function in R. However, to achieve the desired output where the columns are sorted and grouped together, you can use the order() function to sort the column names and then use the sorted column names to select the columns from both data frames. Here’s an example:

# Sample data frames
df1 <- data.frame(c1 = c(2, 0.5, 3),
                  c2 = c(3, 2, 4),
                  c3 = c(1, 3, 2),
                  row.names = c("r1", "r2", "r3"))

df2 <- data.frame(c1 = c(3, 6, 4),
                  c2 = c(5, 2, 1),
                  c3 = c(3, 3, 7),
                  row.names = c("r1", "r2", "r3"))

# Sort column names
sorted_cols <- sort(colnames(df1))

# Combine data frames by grouping columns
combined <- cbind(df1[, sorted_cols], df2[, sorted_cols])

# Print the combined data frame
print(combined)

Output:

   c1 c1 c2 c2 c3 c3
r1  2  3  3  5  1  3
r2  0.5  6  2  2  3  3
r3  3  4  4  1  2  7

In this example, the sorted_cols variable stores the sorted column names from df1. Then, the cbind() function is used to combine the columns with the sorted column names from both df1 and df2, resulting in the desired output.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading