check whether all elements of a list are in equal in R
How about
allSame <- function(x) length(unique(x)) == 1
allSame(test_true)
# [1] TRUE
allSame(test_false)
# [1] FALSE
As @JoshuaUlrich pointed out below, unique
may be slow on lists. Also, identical
and unique
may use different criteria. Reduce
is a function I recently learned about for extending pairwise operations:
identicalValue <- function(x,y) if (identical(x,y)) x else FALSE
Reduce(identicalValue,test_true)
# [1] 1 2 3
Reduce(identicalValue,test_false)
# [1] FALSE
This inefficiently continues making comparisons after finding one non-match. My crude solution to that would be to write else break
instead of else FALSE
, throwing an error.
Test if all elements of a list (lists themselves) are equal
This is the canonical method for determining whether all items in a list are the same:
length(unique(object))==1
In your case:
> length( unique( eventual.list ) )
[1] 2
> length( unique( eventual.list ) ) == 1
[1] FALSE
The help page for unique
might have led one to think that lists would not be handled, until one reflects on this result that surprised me when I encountered it the first time:
is.vector( eventual.list )
[1] TRUE
So lists are technically vectors in R parlance, they're just not 'atomic' vectors, they are "recursive".
> is.atomic(eventual.list)
[1] FALSE
> is.recursive(eventual.list)
[1] TRUE
Is there a better way to check if all elements in a list are named?
I am not sure if the following base R code works for your general cases, but it seems work for the ones in your post.
Define a function f
to check the names
f <- function(lst) length(lst) == sum(names(lst) != "",na.rm = TRUE)
and you will see
> f(x)
[1] TRUE
> f(y)
[1] FALSE
> f(z)
[1] FALSE
Test for equality between all members of list
1) IF the list has only two components as in the question then try this:
identical(some_list[[1]], some_list[[2]])
## [1] TRUE
2) or for a general approach with any number of components try this:
all(sapply(some_list, identical, some_list[[1]]))
## [1] TRUE
L <- list(1, 2, 3)
all(sapply(L, identical, L[[1]]))
## [1] FALSE
3) Reduce This is a bit verbose compared to the prior solution but since there was a discussion of Reduce
, this is how it could be jmplemented:
Reduce(function(x, y) x && identical(y, some_list[[1]]), init = TRUE, some_list)
## [1] TRUE
Reduce(function(x, y) x && identical(y, L[[1]]), init = TRUE, L)
## [1] FALSE
r - check is all elements of a list match a vector exactly
We can use lapply
:
lapply(list_1, function(x) if(all(x %in% vec_1)) TRUE else setdiff(x, vec_1))
#$x
#[1] TRUE
#$y
#[1] "foo" "bars"
how can i check if all the elements of list are integers in r?
Try the code below
> all(sapply(List_example, `%%`, 1) == 0)
[1] TRUE
how to see if any element of a list contains only a certain value in R
We may need to wrap with all
- loop over the list
of matrices with sapply
, create a logical expression (x == 0
), wrap with all
to return a single TRUE/FALSE - if all values excluding NAs (na.rm = TRUE
) are 0, this returns TRUE or else FALSe
sapply(my_list, function(x) all(x == 0, na.rm = TRUE))
Test for equality among all elements of a single numeric vector
I use this method, which compares the min and the max, after dividing by the mean:
# Determine if range of vector is FP 0.
zero_range <- function(x, tol = .Machine$double.eps ^ 0.5) {
if (length(x) == 1) return(TRUE)
x <- range(x) / mean(x)
isTRUE(all.equal(x[1], x[2], tolerance = tol))
}
If you were using this more seriously, you'd probably want to remove missing values before computing the range and mean.
R: how to check if vector elements are the same
You can simply check the number of unique values:
length(unique(ranks)) == 1
Find out if there's equal elements in a list in a column in R
Here's a function that, I think, deals with all of the specifications in the comments. It should find any set of values that always show up together, whether pairs, triplets, etc... To showcase this, I modified the data somewhat.
example <- tibble::tribble(
~x,
"a,d,e",
"a,e,d",
"b,e,f,a,c",
"a,e",
"b,c,d,f"
)
find_equal <- function(data){
all_vals <- strsplit(data, ",") %>% unlist %>% unique
library(stringr)
dums <- sapply(all_vals, function(x)str_detect(data, x))
eq <- lapply(1:ncol(dums), function(i)apply(dums[,-i], 2, function(x)all(dums[,i] == x)))
names(eq) <- all_vals
out <- sapply(1:length(eq), function(i){
if(any(eq[[i]]))paste(sort(c(names(eq)[i], names(eq[[i]])[which(eq[[i]])])), collapse=",")
else{NULL}
})
out <- unlist(out)
unique(out)
}
find_equal(example$x)
# [1] "a,e" "b,c,f"
Related Topics
Find the Nearest X,Y Coordinate Using R
How to Use Gsub() on Each Element of a Data Frame
Roracle Not Working in R Studio
R: Save All Data.Frames in Workspace to Separate .Rdata Files
Math of Tm::Findassocs How Does This Function Work
How to Add Main Title and Manipulating Axis Labels in Ggplot2 in Rstudio
Shiny - How to Change the Font Size in Select Tags
How to Select_If in Dplyr, Where the Logical Condition Is Negated
Exporting Multiple Panels of Plots and Data to *.Png (In the Style Layout() Works Within R)
How to Use an R Script from Github
Remove Some of the Axis Labels in Ggplot Faceted Plots
Running an R Script Using a Windows Shortcut
Remove Multiple Patterns from Text Vector R