在另一个数据框中存在数据帧中的R

发布于 2025-02-11 14:05:01 字数 414 浏览 3 评论 0原文

我有两个dataframes a和b:

A

     x          y
1   0.0  0.0000000
2   0.5  0.8000000
3  -0.5  0.8000000
4  -1.0  0.0000000
5  -0.5 -0.8000000
6   0.5 -0.8000000
7   1.0  0.0000000
8   1.5  0.8000000

B

     x          y
1  -1.0  0.0000000
2   0.5 -0.8000000
3   3.0  0.0000000

我只想在B中存在的A中提取的行索引,以便最终结果将是:

C(4,6)

我应该如何做到这一点?

I have two dataframes A and B:

A

     x          y
1   0.0  0.0000000
2   0.5  0.8000000
3  -0.5  0.8000000
4  -1.0  0.0000000
5  -0.5 -0.8000000
6   0.5 -0.8000000
7   1.0  0.0000000
8   1.5  0.8000000

B

     x          y
1  -1.0  0.0000000
2   0.5 -0.8000000
3   3.0  0.0000000

I want to extract just the row indexes in A that exist in B so that the final result will be:

c(4,6)

How should I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

温柔少女心 2025-02-18 14:05:01

交互可用于在多列上使用%中的%。

which(interaction(A) %in% interaction(B))
#[1] 4 6

数据

A <- read.table(header=TRUE, text="     x          y
1   0.0  0.0000000
2   0.5  0.8000000
3  -0.5  0.8000000
4  -1.0  0.0000000
5  -0.5 -0.8000000
6   0.5 -0.8000000
7   1.0  0.0000000
8   1.5  0.8000000")

B <- read.table(header=TRUE, text="     x          y
1  -1.0  0.0000000
2   0.5 -0.8000000
3   3.0  0.0000000")

interaction could be used to use %in% on multiple columns.

which(interaction(A) %in% interaction(B))
#[1] 4 6

Data

A <- read.table(header=TRUE, text="     x          y
1   0.0  0.0000000
2   0.5  0.8000000
3  -0.5  0.8000000
4  -1.0  0.0000000
5  -0.5 -0.8000000
6   0.5 -0.8000000
7   1.0  0.0000000
8   1.5  0.8000000")

B <- read.table(header=TRUE, text="     x          y
1  -1.0  0.0000000
2   0.5 -0.8000000
3   3.0  0.0000000")
我为君王 2025-02-18 14:05:01

将另一列添加到A只是一个序列的A,然后合并

> A$c=1:nrow(A)
> merge(A,B)$c
[1] 4 6

Add another column to A which is just a sequence and then merge

> A$c=1:nrow(A)
> merge(A,B)$c
[1] 4 6
贩梦商人 2025-02-18 14:05:01

使用join.keys来自plyr的函数:

library(plyr)
with(join.keys(A, B), which(x %in% y))

输出:

[1] 4 6

Using the join.keys function from plyr:

library(plyr)
with(join.keys(A, B), which(x %in% y))

Output:

[1] 4 6
ゝ偶尔ゞ 2025-02-18 14:05:01

一种可能的方法是计算出现True两次的次数。如果列变得更宽,则可以在它们上映射。

which(`+`(df1$x %in% df2$x, df1$y %in% df2$y) == 2)

4 6

One possible way is to count the number of times TRUE appears twice. If the columns get wider you can map over them.

which(`+`(df1$x %in% df2$x, df1$y %in% df2$y) == 2)

4 6

夏至、离别 2025-02-18 14:05:01

使用外部

which(rowSums(outer(1:nrow(A), 1:nrow(B), Vectorize(\(i, j) all(A[i, ] == B[j, ])))) == 1)
# [1] 4 6

或循环的

r <- c()
for (j in seq_len(nrow(B))) {
  for (i in seq_len(nrow(A))) {
    if (all(A[i, ] == B[j, ])) r <- c(r, i)
  }
}
r
# [1] 4 6

Using outer

which(rowSums(outer(1:nrow(A), 1:nrow(B), Vectorize(\(i, j) all(A[i, ] == B[j, ])))) == 1)
# [1] 4 6

or a for loop

r <- c()
for (j in seq_len(nrow(B))) {
  for (i in seq_len(nrow(A))) {
    if (all(A[i, ] == B[j, ])) r <- c(r, i)
  }
}
r
# [1] 4 6
甜宝宝 2025-02-18 14:05:01
library(data.table)
setDT(A)
setDT(B)
A[fintersect(A, B), on = names(A), which = TRUE]
# [1] 4 6


library(dplyr)
A |> 
  mutate(row = row_number()) |>
  inner_join(B, by = names(A)) |>
  pull(row)
# [1] 4 6

数据

A = data.frame(
  x = c(0, 0.5, -0.5, -1, -0.5, 0.5, 1, 1.5), 
  y = c(0, 0.8, 0.8, 0, -0.8, -0.8, 0, 0.8))
B = data.frame(
  x = c(-1, 0.5, 3), y = c(0, -0.8, 0)
)
library(data.table)
setDT(A)
setDT(B)
A[fintersect(A, B), on = names(A), which = TRUE]
# [1] 4 6


library(dplyr)
A |> 
  mutate(row = row_number()) |>
  inner_join(B, by = names(A)) |>
  pull(row)
# [1] 4 6

Data

A = data.frame(
  x = c(0, 0.5, -0.5, -1, -0.5, 0.5, 1, 1.5), 
  y = c(0, 0.8, 0.8, 0, -0.8, -0.8, 0, 0.8))
B = data.frame(
  x = c(-1, 0.5, 3), y = c(0, -0.8, 0)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文