how to combine two dataframe in r

To combine two dataframes in R, you can use the merge() or join() function depending on your specific use case. Here's how you can use these functions:

merge()

Suppose we have two dataframes df1 and df2:

main.r
df1 <- data.frame(id=c(1,2,3), name=c('John', 'Jane', 'Jim'), stringsAsFactors = FALSE)
df2 <- data.frame(id=c(2,3,4), age=c(25,30,35), stringsAsFactors = FALSE)
162 chars
3 lines

You can merge them using the merge() function as follows:

main.r
merged_df <- merge(df1, df2, by='id', all=TRUE)
48 chars
2 lines

Here, by='id' specifies that we want to merge the dataframes based on the id column. all=TRUE ensures that rows from both dataframes are included in the merged dataframe.

join()

Alternatively, we can use the join() function from the dplyr package. First, install and load the dplyr package:

main.r
install.packages('dplyr') # if not already installed
library(dplyr)
68 chars
3 lines

Then, we can use the left_join() function to combine the two dataframes:

main.r
joined_df <- left_join(df1, df2, by='id')
42 chars
2 lines

Here, by='id' specifies that we want to join the dataframes based on the id column. left_join() ensures that all rows from the first dataframe (df1) are included in the joined dataframe.

I hope this helps!

gistlibby LogSnag