保留仅出现一次的向量元素或矩阵列

发布于 2025-02-13 03:53:57 字数 453 浏览 3 评论 0原文

我有一个矩阵:

A<-t(matrix(
c(0, 0, 1,
  0, 0, 0,
  0, 0, 1,
  0, 0, 1,
  0, 0, 0,
  1, 1, 0), 3, 6))

我需要保留仅出现一次的列。因此,预期的结果只是第三列:(1,0,1,1,0,0,0)。

我发现 unique 重复的功能,但我需要更强大的东西可以删除所有出现不止一次的列(在我的示例中,第一个和第二个)。

I have a matrix:

A<-t(matrix(
c(0, 0, 1,
  0, 0, 0,
  0, 0, 1,
  0, 0, 1,
  0, 0, 0,
  1, 1, 0), 3, 6))

and I need to keep columns that appear only once. So, the expected result is just the 3rd column: (1, 0, 1, 1, 0, 0).

I have found the unique and duplicated functions but I need something stronger to delete all columns that appear more than once (in my example the 1st and 2nd).

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

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

发布评论

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

评论(3

红墙和绿瓦 2025-02-20 03:53:57

看来我们需要双重重复

A[, !(duplicated(t(A)) | duplicated(t(A), fromLast = TRUE)), drop = FALSE]
     [,1]
[1,]    1
[2,]    0
[3,]    1
[4,]    1
[5,]    0
[6,]    0

这个想法也适用于向量。

x <- c(1, 1, 2, 3, 4, 3, 4, 5)

x[!(duplicated(x) | duplicated(x, fromLast = TRUE))]
[1] 2 5

Looks like we need a double duplicated.

A[, !(duplicated(t(A)) | duplicated(t(A), fromLast = TRUE)), drop = FALSE]
     [,1]
[1,]    1
[2,]    0
[3,]    1
[4,]    1
[5,]    0
[6,]    0

The idea applies to a vector, too.

x <- c(1, 1, 2, 3, 4, 3, 4, 5)

x[!(duplicated(x) | duplicated(x, fromLast = TRUE))]
[1] 2 5
情绪 2025-02-20 03:53:57

我们可以使用汇总在下面尝试基本R代码,以汇总a的唯一信息

with(
  aggregate(
    . ~ id,
    data.frame(id = c(col(A)), val = c(A)),
    toString
  ),
  A[, ave(id, val, FUN = length) == 1, drop = FALSE]
)

,或

A[
  ,
  with(
    aggregate(
      . ~ id,
      data.frame(id = c(col(A)), val = c(A)),
      toString
    ),
    ave(id, val, FUN = length) == 1
  ),
  drop = FALSE
]

等效

     [,1]
[1,]    1
[2,]    0
[3,]    1
[4,]    1
[5,]    0
[6,]    0

We can try the base R code below using aggregate to summarize the uniqueness info of A by columns

with(
  aggregate(
    . ~ id,
    data.frame(id = c(col(A)), val = c(A)),
    toString
  ),
  A[, ave(id, val, FUN = length) == 1, drop = FALSE]
)

or equivalently

A[
  ,
  with(
    aggregate(
      . ~ id,
      data.frame(id = c(col(A)), val = c(A)),
      toString
    ),
    ave(id, val, FUN = length) == 1
  ),
  drop = FALSE
]

which gives

     [,1]
[1,]    1
[2,]    0
[3,]    1
[4,]    1
[5,]    0
[6,]    0
一个人的旅程 2025-02-20 03:53:57

另一个可能的解决方案:

A[,apply(sapply(which(duplicated(A, MARGIN = 2)),
  \(x) sapply(1:ncol(A), \(y) all(A[,x] == A[,y]))), 1, \(z) all(!z)), drop = F]

#>      [,1]
#> [1,]    1
#> [2,]    0
#> [3,]    1
#> [4,]    1
#> [5,]    0
#> [6,]    0

或:

A[,colSums(outer(which(duplicated(A, MARGIN = 2)), 1:ncol(A),
    Vectorize(\(x, y) all(A[,x] == A[,y])))) == 0, drop = F]

#>      [,1]
#> [1,]    1
#> [2,]    0
#> [3,]    1
#> [4,]    1
#> [5,]    0
#> [6,]    0

Another possible solution:

A[,apply(sapply(which(duplicated(A, MARGIN = 2)),
  \(x) sapply(1:ncol(A), \(y) all(A[,x] == A[,y]))), 1, \(z) all(!z)), drop = F]

#>      [,1]
#> [1,]    1
#> [2,]    0
#> [3,]    1
#> [4,]    1
#> [5,]    0
#> [6,]    0

Or:

A[,colSums(outer(which(duplicated(A, MARGIN = 2)), 1:ncol(A),
    Vectorize(\(x, y) all(A[,x] == A[,y])))) == 0, drop = F]

#>      [,1]
#> [1,]    1
#> [2,]    0
#> [3,]    1
#> [4,]    1
#> [5,]    0
#> [6,]    0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文