HollowBastion
HollowBastion

Reputation: 223

Checking that all the columns with the same name, are equal in R?

Say I have two dataframes:

df1

row.names c1 c3 c6 c8 c9
r1 0 3 4 1 1
r2 1 4 5 7 3
r3 0 1 3 4 6

df2

row.names c1 c4 c6 c8 c9
r1 0 2 4 1 1
r2 1 5 5 7 3
r3 0 0 3 4 7

And I want to check that those columns in the two dataframes, with the same name, are equal (as in all their values are exactly the same)?

In this case, c1, c6, c8, and c9 will be checked because both data frames have these column names but only c1, c6, c8 will be true for all equal values, while c9 will be false (6 in df1 has been changed to a 7 in df2)?

It would be a great bonus if somehow it could indicate if all columns with the same column names, are equal. So instead of having one true for every column, have one overall true?

I'm not too familiar with R so not sure how do-able this problem is.. any help would be appreciated!

Upvotes: 1

Views: 1206

Answers (2)

Rich Scriven
Rich Scriven

Reputation: 99371

You could use mapply(). First we use intersect() to find the matching column names, then run identical() over each column.

i <- intersect(names(df1), names(df2))
mapply(identical, df1[i], df2[i])
#   c1    c6    c8    c9 
# TRUE  TRUE  TRUE FALSE 

where

df1 <- read.table(text = "c1 c3 c6 c8 c9
 r1 0 3 4 1 1
 r2 1 4 5 7 3
 r3 0 1 3 4 6", header = TRUE)
df2 <- read.table(text = "c1 c4 c6 c8 c9
 r1 0 2 4 1 1
 r2 1 5 5 7 3
 r3 0 0 3 4 7", header = TRUE)

Upvotes: 4

user2357031
user2357031

Reputation:

Another option is all.equal:

i <- intersect(names(df1), names(df2))
all.equal(df1[i], df2[i])
[1] "Component “c9”: Mean relative difference: 0.1666667"

So, in this case, not all values in column c9 are the same.

Upvotes: 4

Related Questions