匹配数据和计数值相同的值 [英] Match data and count number of same value

查看:173
本文介绍了匹配数据和计数值相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据集(数据帧)。我想根据每个列名找到这些数据集之间的匹配值。

  intput1<  -  structure(list = c(1L,0L,1L,0L),B = c(2L,2L,1L,1L),
C = c(3L,1L,1L,3L)),Names = c ,B,C),class =data.frame,
row.names = c(1,2,3,4))

#ABC
#1 1 2 3
#2 0 2 1
#3 1 1 1
#4 0 1 3

输入2结构(列表(A = c(1L,3L,1L,0L),B = c(1L,2L,0L,1L),
C = c(2L,2L,1L,2L) ),.Names = c(A,B,C),class =data.frame,
row.names = c(1,2,3 4))

#ABC
#1 1 1 2
#2 3 2 2
#3 1 0 1
#4 0 1 2

预期输出:

 #colnames.1 colnames.2 match 
#1 AA 3
#2 AB 1
#3 AC 1
#4 BA 1
#5 BB 2
#6 BC 2
#7 CA 1
#8 CB 0
#9 CC 1

其中最后一列是匹配数。

解决方案

这是一种可能性:

  f<  -  function(x,y)sum(x == y)

oo< - outer(input1,input2,Vectorize(f))

#ABC
#A 3 1 1
#B 1 2 3
#C 1 0 1

as.data.frame.table(oo)

#Var1 Var2 Freq
#1 AA 3
#2 BA 1
#3 CA 1
#4 AB 1
#5 BB 2
#6 CB 0
#7 AC 1
#8 BC 3
#9 CC 1


I have two data sets (data frames). I would like to find match values between these data sets based on each column names.

intput1 <- structure(list(A = c(1L, 0L, 1L, 0L), B = c(2L, 2L, 1L, 1L), 
C = c(3L, 1L, 1L, 3L)), .Names = c("A", "B", "C"), class = "data.frame", 
row.names = c("1", "2", "3", "4"))

#     A    B   C
#1    1    2   3
#2    0    2   1
#3    1    1   1
#4    0    1   3

input2 <- structure(list(A = c(1L, 3L, 1L, 0L), B = c(1L, 2L, 0L, 1L), 
C = c(2L, 2L, 1L, 2L)), .Names = c("A", "B", "C"), class = "data.frame", 
row.names = c("1", "2", "3", "4"))

#     A    B   C
#1    1    1   2
#2    3    2   2
#3    1    0   1
#4    0    1   2

Expected output:

#     colnames.1   colnames.2   match
#1             A           A        3
#2             A           B        1
#3             A           C        1
#4             B           A        1
#5             B           B        2
#6             B           C        2
#7             C           A        1
#8             C           B        0
#9             C           C        1

where the final column is the number of matches.

解决方案

Here is a possibility:

f <- function (x, y) sum(x == y)

oo <- outer(input1, input2, Vectorize(f))

#  A B C
#A 3 1 1
#B 1 2 3
#C 1 0 1

as.data.frame.table(oo)

#  Var1 Var2 Freq
#1    A    A    3
#2    B    A    1
#3    C    A    1
#4    A    B    1
#5    B    B    2
#6    C    B    0
#7    A    C    1
#8    B    C    3
#9    C    C    1

这篇关于匹配数据和计数值相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆