R - 比较两个不同长度的数据帧以获取两列中的相同值

R - Compare two data frames of different length for same values in two columns

这是一个关于如何比较具有不同长度的两个不同数据帧的几列的问题。

我有两个不同长度的数据帧(来自接收器 1 (rec1) 和接收器 2 (rec2) 的数据),其中包含 4 艘不同船只的位置:

rec1 <- data.frame(name = sample (c("Nina", "Doug", "Alli", "Steve"), 20, replace = TRUE), 
                lon = sample (1:20), 
                lat = sample (1:10)
                )    
rec2 <- data.frame(name = sample (c("Nina", "Doug", "Alli", "Steve"), 30, replace = TRUE), 
                lon = sample (1:30),
                lat = sample (1:30)
                )

它们包含不同的名称(船名,两者名称相同)和经度 (lon) 以及纬度 (lat) 坐标。

我正在尝试比较两个 df,以查看 "lon" 和 "lat" 中有多少值与每艘船匹配(即两个接收器拾取相同位置的频率)

基本上,我试图找出每个接收器的性能以及有多少数据点重叠(例如百分比)。

我不确定如何做到最好,欢迎提出任何建议。非常感谢!!!

你没有构建一个非常有用的测试用例,但这里有一个方法:

> both <- rbind(data.frame(grp="A", rec1[, 2:3]), data.frame(grp="B", rec2[, 2:3]))
> with(both, table( duplicated(both[,2:3]), grp))
       grp
         A  B
  FALSE 20 30

这是一个经过修改且可重现的测试用例以及我的答案。我将测试集设计为包含匹配的组合和一些不匹配的组合。

rec1 <- data.frame(shipName = rep(c("Nina", "Doug", "Alli", "Steve"), each = 5), 
                lon = rep.int(c(1:5), 4), 
                lat = rep.int(c(11:15), 4)
                )    
rec2 <- data.frame(shipName = rep(c("Nina", "Doug", "Alli", "Steve"), each = 7), 
                lon = rep.int(c(2, 3, 4, 4, 5, 5, 6), 4),
                lat = rep.int(c(12, 13, 14, 14, 15, 15, 16), 4)
                )

print(rec1)
print(rec2)

#Merge the two data frames together, keeping only those combinations that match
m <- merge(rec1, rec2, by = c("shipName", "lon", "lat"), all = FALSE)

print(m)

如果您想计算每个组合出现的次数,请尝试以下操作。 (有不同的聚合方式。有些是 here。下面是我的首选方法,它需要您安装 data.table。这是一个很棒的工具,所以如果您还没有安装它,您可能想要安装它'还没到。)

library(data.table)

#Convert to a data table and optionally set the sort key for faster processing
m <- data.table(m)
setkey(m, shipName, lon, lat)

#Aggregate and create a new column called "Count" with the number of
    #observations in each group (.N)
m <- m[, j = list("Count" = .N), by = list(shipName, lon, lat)]

print(m)

#If you want to return to a standard data frame rather than a data table:
m <- data.frame(m)

在基数 R 中进行这种比较的最简单方法是使用 merge

试试这个:

# Set the RNG so sample() produces the same output and this example is reproducible
set.seed(720) 

rec1 <- data.frame(name = sample (c("Nina", "Doug", "Alli", "Steve"), 20, replace = TRUE), 
            lon = sample (1:20), 
            lat = sample (1:10)
            )    
rec2 <- data.frame(name = sample (c("Nina", "Doug", "Alli", "Steve"), 30, replace = TRUE), 
            lon = sample (1:30),
            lat = sample (1:30)
            )

merged <- merge(x = rec1,
                y = rec2,
                by = c("name","lat","lon"))

print(merged)

合并后的数据框将包含所有三列都匹配的所有情况(在本例中为一列)。然后,您可以执行类似 table(merged$name) 的操作来计算每个名称在合并数据中出现的次数。

虽然,你的问题让我想知道...这里一定有某种时间因素,是吗?如果您在数据中包含测量时间,则可以通过 nametime 合并,然后计算测量的 latlon的区别。

编辑:

我觉得如果我不提到很棒的 dplyr 包,我会失职,这使得像这样的分析变得非常简单。上面的唯一名称值的合并和计数是通过这个简单的一行代码实现的:

inner_join(rec1, rec2) %>% count(name)