-5

Say I have two dataframes, df1 and df2, that look like this:

student     x    y    teacher   value
s.name1     4    5    t.name1   1
s.name2     3    3    t.name1   2
s.name2     2    1    t.name2   3
s.name3     5    5    t.name3   1

and that I would like to combine these two dataframes based on the contents of df$student, df$x, df$y, and df$teacher being the same, but NOT df$value. What is the best approach for this? Thanks.

Edit: Figured it out using merge(), posted the solution.

soosus
  • 1,211
  • 4
  • 18
  • 27

3 Answers3

0

Simplest way is to use rbind. This has been answered in Simplest way to get rbind to ignore column names. Just use

rbind(db1, setNames(rev(db2), names(db1)))

In case names are different.

boski
  • 2,437
  • 1
  • 14
  • 30
  • Both dataframes have the same column names. What if I only want to match based on a subset of the columns in each dataframe, but include other columns which may have different contents? – soosus Nov 28 '18 at 15:01
0

Figured it out:

merged.df <- merge(df1, df2 ,by=c("student", "teacher", "x", "y"))
soosus
  • 1,211
  • 4
  • 18
  • 27
0

It depends on what you exactly want to achieve.

Example

df1 <- data.frame(student = c("A", "B", "C"), teacher = c("Mike", "Leslie", "David"), score = c(80, 90, 100))

df2 <- data.frame(student = c("A", "B", "D", "E", "F"), score = c(100, 80, 90, 100, 75), teacher = c("Mike", "Leslie", "Mike", "Leslie", "Bill"))

data <- full_join(df1, df2, by = c("student", "teacher"))

There are many ways to do achieve different results. You can try merge(), too.

A page to consult https://rpubs.com/williamsurles/293454

yearntolearn
  • 1,064
  • 2
  • 17
  • 36