Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if an R object is a named numeric vector?

Tags:

validation

r

I'd like to know if there is a simple and robust way to test if an object is a named numeric vector. Here is an example:

data <- c(2.3, 3.5, 5.7)
typeof(data)
#[1] "double"
mode(data)
#[1] "numeric"
str(data)
# num [1:3] 2.3 3.5 5.7
is.vector(data)
#[1] TRUE
is.numeric(data)
#[1] TRUE

So as you can see from the above, I have created a numeric vector. Now I assign names to the elements.

named_vec <- structure(data, names = c('a', 'b', 'c'))
named_vec <- structure(data, names = c('a', 'b', 'c'))
str(named_vec)
# Named num [1:3] 2.3 3.5 5.7
# - attr(*, "names")= chr [1:3] "a" "b" "c"
names(named_vec)
#[1] "a" "b" "c"

I could use a few compound conditions such as:

if(is.vector(named_vec) & is.numeric(named_vec) & !is.null(names(named_vec)))           {
   "named_vec is a named numeric vector"
 } else {
   "named_vec is not a named numeric vector"
 }
#[1] "named_vec is a named numeric vector"

Or something like this, but it feels like it could fall prey to corner cases:

length(names(named_vec)) == length(named_vec)
#[1] TRUE
names(data)
#NULL

I'm looking for something that is concise and robust.

like image 492
Ursus Frost Avatar asked Nov 15 '25 00:11

Ursus Frost


2 Answers

The only problem I see in your firt solution (the if clause) is that the names of the vector could be incomplete, and I do not know if thats acceptable in your case.

In the second solution, a non-numeric named vector would pass the verification, right? And it seems to me that this is not what you desire.

If you could provide more details about what you want exactly to do with this verification, I could help you a little more.

It is also easier to do

is.vector(f, mode = "numeric")

Thus you verifie both conditions (vector and numeric). Read is.vector help.

like image 66
droubi Avatar answered Nov 17 '25 17:11

droubi


My answer simply combines the comments of @nicola and @Gregor along with the answer provided by @droubi:

Create a new function IsNamedVector. Below is example:

Create Example Data Set

> data <- c(2.3, 3.5, 5.7)
> typeof(data)
[1] "double"
> mode(data)
[1] "numeric"
> str(data)
 num [1:3] 2.3 3.5 5.7
> is.numeric(data)
[1] TRUE
> is.vector(data)
[1] TRUE
> named_vec <- structure(data, names = c('a', 'b', 'c'))
> str(named_vec)
 Named num [1:3] 2.3 3.5 5.7
 - attr(*, "names")= chr [1:3] "a" "b" "c"
> names(named_vec)
[1] "a" "b" "c"

Create Custom Function

> IsNamedVector <- function(VECTOR) {
+   is.vector(VECTOR) & is.numeric(VECTOR) & !is.null(names(VECTOR)) & 
+      !any(is.na(names(VECTOR)))
+ }

Test Function

> length(names(named_vec)) == length(named_vec)
[1] TRUE
> names(data)
NULL
> !all(is.na(names(named_vec)))
[1] TRUE
> missing_names_vec <- structure(data, names = c('a', 'b'))
> str(missing_names_vec)
 Named num [1:3] 2.3 3.5 5.7
 - attr(*, "names")= chr [1:3] "a" "b" NA
> IsNamedVector(named_vec)
[1] TRUE
> IsNamedVector(missing_names_vec)
[1] FALSE

Thanks for the help.

like image 38
Ursus Frost Avatar answered Nov 17 '25 16:11

Ursus Frost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!