Skip to content
Prev 172693 / 398506 Next

Odp: nice way to find or not a value (problem with numeric(0))

Hello Petr,

In fact spec is data.frame with a column called code (containing numerical
values) and some other columns called data1, data2, ... containing data for
each equipment (that is for each code).

But I don't have the data for all my devices. It means that some 'code' are
not in spec$Code.
In that case I want to assign a default value to the data.

for example :
spec$Code     spec$data1
4                           12.5
8                            20.2

Then, with code=4 
spec$data1[spec$Code==code]*(code %in%specmodules$Code) + 1*(!code 
%in% specmodules$Code) 
gives 12.5

But with code=654,
I get numeric(0) instead of 1
because 
this value is not in spec$Code and returns numeric(0).

I hope it is clearer (is it ?) and that you could find a nice way to write
my test (the if-test works but is not very elegant).

Thanks in adance for you help, 
Ptit Bleu.
Petr Pikal wrote: