【问题标题】:Compare values in data frame and return results比较数据框中的值并返回结果
【发布时间】:2014-07-17 19:50:14
【问题描述】:

我需要比较两个数据帧并生成比较结果的输出。尺寸相同。列和行顺序匹配。我想比较两个数据帧之间的每个对应单元格,并确定它们是否包含相同的值或不同的值。如果值不同,我需要知道这两个值是否属于我定义的特定向量,或者它们是否来自 2 个不同的向量。我在下面提供了示例代码。

我无法在论坛中找到完全符合我需要的任何内容,主要是因为我需要知道值何时不同,根据我提供的标准它们有何不同。

#Possible Value Types for the Data Frames
typeA = c("Green", "Blue", "Purple")
typeB = c("Red", "Orange", "Yellow")

#Create Data Frames to Compare
df1 = as.data.frame(cbind(rbind("Green","Red","Yellow"), 
            rbind("Green", "Purple", "Red"), 
            rbind("Orange", "Orange",NA), 
            rbind(NA,"Red","Purple")))

df2 = as.data.frame(cbind(rbind("Green","Red","Yellow"), 
                          rbind(NA, "Purple", "Yellow"), 
                          rbind("Blue", "Orange",NA), 
                          rbind("Blue","Red","Green")))

#Data frames compared must have identical dimensions
###INSERT FUNCTION HERE
myfunction = function(df1,df2){
  #compare corresponding cells and provide output based on match
  #example: compare cell df1[1,1] to df2[1,1]
  #if either df1[1,1] or df2[1,1] is NA then return NA, else...
    #if df1[1,1] matches df2[1,1] then return "Match"
    #if df1[1,1] does not match df2[1,1] but they are both in vector typeB then return "SAMEGROUP"
    #if df1[1,1] does not match df2[1,1] and one is in vector typeA and the other in typeB then return "DIFFGROUP"
}

###RUN FUNCTION
df.out = myfunction(df1,df2)

#expected output
#Match: The values in df1 and df2 for that cell are identical
#SAMEGROUP: The values in df1 and df2 for that cell are different, but
##they come from the same group (typeA or typeB)
#DIFFGROUP: The values in df1 and df2 for that cell are different, and
##they come from different groups (one from typeA, one from typeB)
#NA: One or both of the corresponding cells in df1 or df2 has an NA

df.out = as.data.frame(cbind(rbind("Match","Match","Match"), 
                          rbind(NA, "Match", "SAMEGROUP"), 
                          rbind("DIFFGROUP", "Match",NA), 
                          rbind(NA,"Match","SAMEGROUP")))

谢谢!

【问题讨论】:

  • 它们有什么不同是什么意思?您似乎没有向我们提供有关您提供的差异标准的任何信息。我可以给你一段代码来确定给定的表格位置是否在相应的表格中匹配,但这只是你的问题。这对我来说似乎是两个问题。
  • 我的数据框中的值可以分组。在我的示例中,typeA 和 typeB。你可以把它想象成一组偶数和一组奇数。然后提出问题,数字是否相同?如果不是,它们是偶数还是奇数,还是一个偶数一个奇数?

标签: r compare match


【解决方案1】:

感谢 jarfa 的建议。这让我走上了正轨。这成功了。

df1 = as.matrix(df1)
df2 = as.matrix(df2)

#ifelse(df1==df2, "match","diff") #test

ifelse(df1==df2, "Match", 
       ifelse(df1 %in% typeA & df2 %in% typeA,"SAMEGROUP",
              ifelse(df1 %in% typeB & df2 %in% typeB, "SAMEGROUP",
                     ifelse(df1 %in% typeA & df2 %in% typeB,"DIFFGROUP",
                            ifelse(df1 %in% typeB & df2 %in% typeA, "DIFFGROUP","TRYAGAIN")))))

【讨论】:

  • 快速解决方案。我刚刚完成了这个问题的解决方案,速度慢了 5 倍(也是矢量化的)。编辑:现在慢 3 倍。大声笑
【解决方案2】:

首先,强制执行您的维度条件:

stopifnot(all.equal(dim(df1), dim(df2)))

对于你的功能:一个天真的、慢的方法是这样的:

for(i in 1:dim(df1)[1])
  for(j in 1:dim(df1)[2])
    #complicated ifelse statement(s)

但这很容易矢量化。见:

a = matrix(1:9, 3)
b = matrix(c(1:8, -1),3)
ifelse(a == b, 'match', 'nomatch')

你的 if/else 肯定会更复杂,但我认为你可以从那里弄清楚。这将是一些嵌套的 ifelse() 函数

编辑:创建一个返回给定值组的函数。然后,声明

groupfun(a) == groupfun(b)

应该只返回一个 TRUES 和 FALSES 的矩阵,这样会很容易使用。

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2019-03-23
    相关资源
    最近更新 更多