# 9-2 Merging Dataframes

# Create dataframe df1

df1<-data.frame(block=c(1,2), code=c("A","B"))

# Create dataframe df2

df2<-data.frame(code=c("A","B"), colour=c("red","blue"))

# Merge dataframes using rbind

df3<-merge(df1,df2)

# Display merged dataframe

df3

# Create dataframe df4
# Header "code" is replaced by header "id"

df4<-data.frame(id=c("A","B"), colour=c("red","blue"))

# Merge dataframes using rbind, telling it which columns to join

df5<-merge(df1,df4, by.x="code", by.y="id")

# Display merged dataframe

df5

# What happens if there are multiple values?

# Create dataframe df6
# Header "code" is replaced by header "id"
# Both "id" values are "A"

df6<-data.frame(id=c("A","A","B"), colour=c("red","blue","green"))

# Merge dataframes using rbind, telling it which columns to join

df7<-merge(df1,df6, by.x="code", by.y="id")

# Display merged dataframe

df7
