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

2009-03-06 Thread Petr PIKAL
Hi

r-help-boun...@r-project.org napsal dne 05.03.2009 15:21:22:

 
 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
 820.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).

Not much. Guess what I get using your code? If you think it is 12.5 you 
are mistaken.

 spec
  code data1
14  12.5
28  20.2
 code=4
 spec$data1[spec$code==code]*(code %in%specmodules$Code) + 1*(!code %in% 
specmodules$Code)
Error in inherits(x, factor) : object specmodules not found


This is why you shall provide reproducible code. Now I still need to only 
**think** what you want to do.

Maybe merge is what you want

Having spec like above and

test-data.frame(code = c(4,5,8,12), value= c(10, 20, 30,40))

then

merge(test, spec, by=code, all.x=T)
  code value data1
1410  12.5
2520NA
3830  20.2
4   1240NA

Gives you new data frame which you can then filter by is.na and which to 
replace NA values with standard ones.

Regards
Petr

 
 Thanks in adance for you help, 
 Ptit Bleu.
 
 
 
 Petr Pikal wrote:
  
  Hi
  
  r-help-boun...@r-project.org napsal dne 04.03.2009 09:11:06:
  
  
  Hello,
  
  I have a data.frame called spec containing data about samples. But I 
  don't
  have these data for all my samples.
  So if I have data (that is code of the sample is in spec$Code), I 
would 
  like
  to assign data1 to the variable m.
  If I don't have this data, I would like to assign 1 to m.
  
  I tried this : 
  m-spec$data1[spec$Code==code]*(code %in%specmodules$Code) + 1*(!code 

  %in%
  specmodules$Code) 
  
  It works when I have the data but if it is not the case I get 
numeric(0)
  instead of 1.
  
  I finally use the following command. It works but I'm sure there is a 

  more
  elegant way.
  if (code %in%spec$Code) m-spec$data1[spec$Code==code] else m-1
  
  It is a bit cryptic what do you want. Above version shall not work as 
it 
  takes only one logical value but you probably have vector of values. 
(We 
  do not know code, spec$Code or any other data you have).
  
  when I try your first construction with some values I have I get 
sensible 
  results so without trying to find out how your data really look like I 

  suggest you to inspect it more closely and/or provide some working 
example 
  demonstrating what you did, what is the result and how the result 
shall 
  look like.
  
  zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
   [1] 110  80  50  50  10   1 120  80  50  20
  zdrz$otac[5]-NA
  zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
   [1] 110  80  50  50   1   1 120  80  50  20
  zdrz$sklon[4]-Inf
  zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
   [1] 110  80  50 Inf   1   1 120  80  50  20
  zdrz$sklon[4]-NA
  zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
   [1] 110  80  50  NA   1   1 120  80  50  20
  
  Regards
  Petr
  
  
  Is there a way to avoid an if-test ?
  
  Thanks for your help,
  Have a good day,
  Ptit Bleu.
  
  -- 
  View this message in context: 
  http://www.nabble.com/nice-way-to-find-or-not-a-
  value-%28problem-with-numeric%280%29%29-tp22325406p22325406.html
  Sent from the R help mailing list archive at Nabble.com.
  
  __
  R-help@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide 
  http://www.R-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.
  
  __
  R-help@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide
  http://www.R-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.
  
  
 
 -- 
 View this message in context: 
http://www.nabble.com/nice-way-to-find-or-not-a-
 value-%28problem-with-numeric%280%29%29-tp22325406p22352529.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
 and provide 

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

2009-03-05 Thread Ptit_Bleu

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
820.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:
 
 Hi
 
 r-help-boun...@r-project.org napsal dne 04.03.2009 09:11:06:
 
 
 Hello,
 
 I have a data.frame called spec containing data about samples. But I 
 don't
 have these data for all my samples.
 So if I have data (that is code of the sample is in spec$Code), I would 
 like
 to assign data1 to the variable m.
 If I don't have this data, I would like to assign 1 to m.
 
 I tried this : 
 m-spec$data1[spec$Code==code]*(code %in%specmodules$Code) + 1*(!code 
 %in%
 specmodules$Code) 
 
 It works when I have the data but if it is not the case I get numeric(0)
 instead of 1.
 
 I finally use the following command. It works but I'm sure there is a 
 more
 elegant way.
 if (code %in%spec$Code) m-spec$data1[spec$Code==code] else m-1
 
 It is a bit cryptic what do you want. Above version shall not work as it 
 takes only one logical value but you probably have vector of values. (We 
 do not know code, spec$Code or any other data you have).
 
 when I try your first construction with some values I have I get sensible 
 results so without trying to find out how your data really look like I 
 suggest you to inspect it more closely and/or provide some working example 
 demonstrating what you did, what is the result and how the result shall 
 look like.
 
 zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
  [1] 110  80  50  50  10   1 120  80  50  20
 zdrz$otac[5]-NA
 zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
  [1] 110  80  50  50   1   1 120  80  50  20
 zdrz$sklon[4]-Inf
 zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
  [1] 110  80  50 Inf   1   1 120  80  50  20
 zdrz$sklon[4]-NA
 zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
  [1] 110  80  50  NA   1   1 120  80  50  20
 
 Regards
 Petr
 
 
 Is there a way to avoid an if-test ?
 
 Thanks for your help,
 Have a good day,
 Ptit Bleu.
 
 -- 
 View this message in context: 
 http://www.nabble.com/nice-way-to-find-or-not-a-
 value-%28problem-with-numeric%280%29%29-tp22325406p22325406.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide 
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 
 

-- 
View this message in context: 
http://www.nabble.com/nice-way-to-find-or-not-a-value-%28problem-with-numeric%280%29%29-tp22325406p22352529.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

2009-03-04 Thread Petr PIKAL
Hi

r-help-boun...@r-project.org napsal dne 04.03.2009 09:11:06:

 
 Hello,
 
 I have a data.frame called spec containing data about samples. But I 
don't
 have these data for all my samples.
 So if I have data (that is code of the sample is in spec$Code), I would 
like
 to assign data1 to the variable m.
 If I don't have this data, I would like to assign 1 to m.
 
 I tried this : 
 m-spec$data1[spec$Code==code]*(code %in%specmodules$Code) + 1*(!code 
%in%
 specmodules$Code) 
 
 It works when I have the data but if it is not the case I get numeric(0)
 instead of 1.
 
 I finally use the following command. It works but I'm sure there is a 
more
 elegant way.
 if (code %in%spec$Code) m-spec$data1[spec$Code==code] else m-1

It is a bit cryptic what do you want. Above version shall not work as it 
takes only one logical value but you probably have vector of values. (We 
do not know code, spec$Code or any other data you have).

when I try your first construction with some values I have I get sensible 
results so without trying to find out how your data really look like I 
suggest you to inspect it more closely and/or provide some working example 
demonstrating what you did, what is the result and how the result shall 
look like.

zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
 [1] 110  80  50  50  10   1 120  80  50  20
zdrz$otac[5]-NA
zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
 [1] 110  80  50  50   1   1 120  80  50  20
zdrz$sklon[4]-Inf
zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
 [1] 110  80  50 Inf   1   1 120  80  50  20
zdrz$sklon[4]-NA
zdrz$sklon*zdrz$otac %in% c(.6,1.2,2)+1*!(zdrz$otac %in% c(.6,1.2,2))
 [1] 110  80  50  NA   1   1 120  80  50  20

Regards
Petr

 
 Is there a way to avoid an if-test ?
 
 Thanks for your help,
 Have a good day,
 Ptit Bleu.
 
 -- 
 View this message in context: 
http://www.nabble.com/nice-way-to-find-or-not-a-
 value-%28problem-with-numeric%280%29%29-tp22325406p22325406.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.