Reputation:
Is there any workaround solution for the following problem?
> bb = list(x=matrix(0,0,0))
> bb = list(x=matrix(0,0,0), y=numeric(0))
> vapply(bb, class, character(1))
Error in vapply(bb, class, character(1)) : values must be length 1,
but FUN(X[[1]]) result is length 2
Upvotes: 1
Views: 131
Reputation: 886968
The first list
element returns two values for class
- matrix
and array
. So we may need to subset by indexing with [1]
(using a lambda/anonymous function) as we specify that the length is 1 and the return value type as character
with character(1)
vapply(bb, function(x) class(x)[1], character(1))
# x y
# "matrix" "numeric"
If we need both values, one option is paste
them together
vapply(bb, function(x) toString(class(x)), character(1))
# x y
#"matrix, array" "numeric"
Or append NA
at the end and extract the first two elements, while specifying the character(2)
vapply(bb, function(x) c(class(x), NA_character_)[1:2], character(2))
#. x y
#[1,] "matrix" "numeric"
#[2,] "array" NA
It can be easily found up with
lapply(bb, class)
#$x
#[1] "matrix" "array"
#$y
#[1] "numeric"
Upvotes: 3