检查列表以找出R中的所有值都相同(Check List to find out all values are same in R)

编程入门 行业动态 更新时间:2024-10-28 08:16:45
检查列表以找出R中的所有值都相同(Check List to find out all values are same in R)

我的清单如下

l = list(c("a", "b", "c"), c("a", "b", "c"), c("a", "b", "c"))

我想使用apply family函数检查它们中的每一个是否包含相同的值。

我想要下面的答案

TRUE, TRUE, TRUE

I have list as follows

l = list(c("a", "b", "c"), c("a", "b", "c"), c("a", "b", "c"))

I want to check that each of them contain same values using apply family functions.

I want following answer

TRUE, TRUE, TRUE

最满意答案

我们可以使用duplicated

duplicated(l)|duplicated(l, fromLast=TRUE) #[1] TRUE TRUE TRUE

如果我们需要比较list元素的所有组合, combn是另一种方式

combn(seq_along(l), 2, FUN= function(x) all(l[[x[1]]] == l[[x[2]]])) #[1] TRUE TRUE TRUE

We can use duplicated

duplicated(l)|duplicated(l, fromLast=TRUE) #[1] TRUE TRUE TRUE

If we need to compare all the combinations of list elements, combn is another way

combn(seq_along(l), 2, FUN= function(x) all(l[[x[1]]] == l[[x[2]]])) #[1] TRUE TRUE TRUE

更多推荐

本文发布于:2023-07-22 20:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1223280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Check   List   find   values

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!