In my R function below, I was wondering how I could get the length of the unique elements (which is 2) of two vectors a and b?
Here is what I tried without success:
foo <- function(...){
L <- list(...)
lengths(unique(unlist(L)))
}
a = rep(c("a", "b"), 30) # Vector `a`
b = rep(c("a", "b"), 20) # Vector `b`
foo(a, b) # the function returns 1 1 instead of 2 2
Use
lapply()orsapply()because your object is a list. I think you might check the difference betweenlength()andlengths(). They both exist but have different abilities. I provide two solutionsfoo1andfoo2: