简体   繁体   English

在 R 中比较和连接数据帧

[英]Compare and join dataframes in R

I have two dataframes that have mostly the same variables and I want to compare the cases of the two dataframes.我有两个具有大部分相同变量的数据框,我想比较这两个数据框的情况。 I want to create a new dataframe with all the cases that are the same in df1 and df2.我想创建一个新的 dataframe,其中所有案例在 df1 和 df2 中都相同。

Cases are assumed to be the same if all values of the variables that are present in both dataframes are the same.如果两个数据帧中存在的所有变量值都相同,则假定案例相同。 There is an exception for the variable "Age", where cases are assumed to be the same if the values have a difference of maximum 1 year and the variable "Time" where a difference of 1 hour is acceptable.变量“Age”有一个例外,如果值的最大差异为 1 年,则假设情况相同,而变量“Time”的差异可以接受 1 小时。

ID1 <- c(100, 101, 102, 103)
V1 <- c(1, 1, 2, 1)
V2 <- c(1, 2, 3, 4)
Age <- c(25, 16, 74, 46)
Time <- c("9:30", "13:25", "17:20", "7:45")
X <- c (1, 3, 4, 1)

df1 <- data.frame(ID1, V1, V2, Age, Time, X)


ID2 <- c(250, 251, 252, 253)
V1 <- c(1, 2, 1, 2)
V2 <- c(1, 2, 2, 4)
Age <- c(26, 55, 16, 80)
Time <- c("9:30", "12:00", "12:55", "18:00")
Y <- c (3, 2, 1, 1)

df2 <- data.frame(ID2, V1, V2, Age, Time, Y)

In this example ID1=100 and ID2=250 are the same and also ID1=101 and ID2=252.在此示例中,ID1=100 和 ID2=250 相同,并且 ID1=101 和 ID2=252。

I'd like to have a new dataframe-output like this one我想要一个像这样的新数据框输出

Note that it is not important if the values for "Age" and "Time" are taken from df1 or df2.请注意,“年龄”和“时间”的值是否取自 df1 或 df2 并不重要。 The important Variables are X an Y.重要的变量是 X 和 Y。

I hope someone can help me out with this problem.我希望有人可以帮助我解决这个问题。 Thanks a lot in advance:)提前非常感谢:)

Kind regards Philip亲切的问候菲利普

In base R:在基础 R 中:

df3 <- merge(df1, subset(df2, select = -c(Age, Time)), by = c("V1", "V2"))
df3[,c("ID1", "ID2", "V1", "V2", "Age", "Time", "X", "Y")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM